Slide 1

Slide 1 text

! Swift, meet Objective-C ! Twitter: @DanToml Email: [email protected]

Slide 2

Slide 2 text

!

Slide 3

Slide 3 text

In the sharp, square bracketed embrace of Objective-C — @AndrewJClark

Slide 4

Slide 4 text

All objective-c written now is technical debt

Slide 5

Slide 5 text

...but so is all of your Swift.

Slide 6

Slide 6 text

You can use them together

Slide 7

Slide 7 text

A whole bunch of new developers are going to be Swift only.

Slide 8

Slide 8 text

Starting out

Slide 9

Slide 9 text

Starting out Bridging Header // // Use this file to import your target's public headers that you would like to expose to Swift. // #import "Person.h" #import "PersonController.h" #import "CoreDataController.h"

Slide 10

Slide 10 text

Objective-C -> Swift

Slide 11

Slide 11 text

The bridging header // ReactiveCocoa-Bridging-Header.h // // Use this file to import your target's public headers that you would like to expose to Swift. // #import "RACCommand.h" #import "RACDisposable.h" #import "RACEvent.h" #import "RACScheduler.h" #import "RACTargetQueueScheduler.h" #import "RACSignal.h" #import "RACSignal+Operations.h" #import "RACStream.h" #import "RACSubscriber.h"

Slide 12

Slide 12 text

Method Names // Objective-C - (instancetype)initWithArray:(NSArray *)array title:(NSString *)title; // Swift init!(array: [AnyObject]!, title: String!)

Slide 13

Slide 13 text

Method Names // Objective-C - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; // Swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

Slide 14

Slide 14 text

Nullability • __nullable • __nonnull • __null_resettable // Properties only. • null_unspecified // Default.

Slide 15

Slide 15 text

Nullability Before // Objective-C @interface XYZList : NSObject - (XYZListItem *)itemWithName:(NSString *)name; @property (copy, readonly) NSArray *allItems; @end // Swift class XYZList : NSObject, NSCoding, NSCopying { func itemWithName(name: String!) -> XYZListItem! @NSCopying var allItems: [AnyObject]! { get } }

Slide 16

Slide 16 text

Nullability After // Objective-C @interface XYZList : NSObject - (XYZListItem * __nullable)itemWithName:(NSString * __nonnull)name; @property (copy, readonly) NSArray * __nonnull allItems; @end // Swift class XYZList : NSObject, NSCoding, NSCopying { func itemWithName(name: String) -> AAPLListItem? @NSCopying var allItems: [AnyObject] { get } }

Slide 17

Slide 17 text

Swift -> Objective-C

Slide 18

Slide 18 text

What can be bridged? • Classes

Slide 19

Slide 19 text

Classes // Swift @objc class MySwiftNSObject { var array: [String] init(array: [String]) { self.array = array; } } // Objective-C @interface MySwiftNSObject @property (nonatomic, copy) NSArray * __nonnull array; - (instancetype)initWithArray:(NSArray * __nonnull)array OBJC_DESIGNATED_INITIALIZER; @end

Slide 20

Slide 20 text

What can be bridged? • Classes • Protocols

Slide 21

Slide 21 text

Protocols @objc protocol Edible { func eat() } @protocol Edible { - (void)eat; }

Slide 22

Slide 22 text

What can be bridged? • Classes • Protocols • Methods

Slide 23

Slide 23 text

Methods func genericFunction(param: T) // Not bridged func defaultParameterFunction(param: String = "Hi") // Not bridged func function(param: String) - (void)function:(NSString * __nonnull)param;

Slide 24

Slide 24 text

What can be bridged? • Classes • Protocols • Methods • Properties

Slide 25

Slide 25 text

Properties // Swift let forname: String var surname: String // Objective-C @property (nonatomic, readonly, copy) NSString * __nonnull forname; @property (nonatomic, copy) NSString * __nonnull surname;

Slide 26

Slide 26 text

Properties KVO (Key Value Observing) dynamic var name: String object.addObserver(self, forKeyPath: "name", options: .New, context: &myContext)

Slide 27

Slide 27 text

What can be bridged? • Classes • Protocols • Methods • Properties • Enums

Slide 28

Slide 28 text

Enums @objc enum MyEnum : Int { case SomeValue = 0 case AnotherValue = 1 } typedef NS_ENUM(NSInteger, MyEnum) { MyEnumSomeValue = 0, MyEnumAnotherValue = 1 };

Slide 29

Slide 29 text

Bridging limitations !!! No: - Structs - Functions - Operators - Generics - Subclassing Swift classes in objective-c

Slide 30

Slide 30 text

Roundup

Slide 31

Slide 31 text

Further Resources Apple • WWDC Session 406 (Integrating Swift with Objective-C) • WWDC Session 407 (Swift Interoperability in Depth) Blog Posts • https://mikeash.com/pyblog/friday-qa-2014-08-15-swift- name-mangling.html

Slide 32

Slide 32 text

Questions? Twitter: @DanToml Email: [email protected]