Swift Objective-C Interop

22

Transcript of Swift Objective-C Interop

Page 1: Swift Objective-C Interop
Page 2: Swift Objective-C Interop

COMBINING SWIFT ANDOBJECTIVE-C

Page 3: Swift Objective-C Interop

AGENDA

Using Objective-C from Swift

Using Swift from Objective-C

Objective-C Behavior in Swift Classes

Page 4: Swift Objective-C Interop

IMPORTING OBJECTIVE-C INTO SWIFT

Page 5: Swift Objective-C Interop

APPLE FRAMEWORKS

No additional setup required!

import UIKitimport Foundation

Page 6: Swift Objective-C Interop

OBJECTIVE-C WITHIN SAME TARGETAdd Objective-C files to the Bridging-Header

Bridging-Header needs to be referenced in build settings

Project-Bridging-Header.h

A.swift B.swift

MyObj.h

Car.h

MyObj.m

Car.m

Page 7: Swift Objective-C Interop

THIRD PARTY FRAMEWORKS

If framework is built as a module:

No additional setup required

import Parse

Page 8: Swift Objective-C Interop

THIRD PARTY FRAMEWORKS

If framework is written in Obj-C

And framework is not built as a module

Add framework header to bridging-header:

#ifndef Makestagram_Makestagram_Bridging_Header_h#define Makestagram_Makestagram_Bridging_Header_h

#import <Parse/Parse.h>

#endif

Page 9: Swift Objective-C Interop

CALLING OBJECTIVE-C FROM SWIFT

Page 10: Swift Objective-C Interop

CALLING OBJ-C FROM SWIFT

Most syntax translates almost 1-1

Special rules for initializers

Page 11: Swift Objective-C Interop

INITIALIZERS

[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

UITableView(frame: CGRectZero, style: .Grouped)

[UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0];

UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0)

Page 12: Swift Objective-C Interop

METHODS

[myTableView insertSubview:mySubview atIndex:2];

myTableView.insertSubview(mySubview, atIndex: 2)

Page 13: Swift Objective-C Interop

PROPERTIES

myTextField.textColor = UIColor.darkGrayColor()

Page 14: Swift Objective-C Interop

OPTIONALITY IN OBJ-C

Since latest release of Objective-C we can

specify whether values can be nil or not

For unaudited APIs: Implicitly unwrapped

Optionals by default!

Page 15: Swift Objective-C Interop

OPTIONALITY IN OBJ-C

// unaudited version -> generates warning+ (UIView *)createViewWithName:(NSString *)name;

// return value and argument are non-optional+ (__nonnull UIView *)createViewWithNameSwiftier:(__nonnull NSString *)name;

// return value and argument are optional+ (__nullable UIView *)createViewWithNameSwiftierNullable:(__nullable NSString *)name;

Page 16: Swift Objective-C Interop

USING SWIFT FROM OBJECTIVE-C

Page 17: Swift Objective-C Interop

SWIFT WITHIN SAME TARGET

AppName-Swift.hA.swift

B.swift

MyObj.h Car.h

MyObj.m Car.m

Page 18: Swift Objective-C Interop

NSOBJECT SUBCLASSES

class SimpleNSObject: NSObject { }

#import "InteropTest-Swift.h"

- (void)test { SimpleNSObject *simple2 = [[SimpleNSObject alloc] init];}

Page 19: Swift Objective-C Interop

SWIFT ROOT CLASS@objc public class Simple { public class func newInstance() -> Simple { return Simple() }}

#import "InteropTest-Swift.h"

- (void)test { Simple *simple = [Simple newInstance];}

Page 20: Swift Objective-C Interop

OBJECTIVE-C BEHAVIOR IN SWIFT CLASSES

Page 21: Swift Objective-C Interop

OBJECTIVE-C BEHAVIOR IN SWIFT CLASSES

class User { dynamic var name: String dynamic func setUp() { //... } }

Use the dynamic keyword to enable Objective-Cfeatures such as KVO and performSelector:

Page 22: Swift Objective-C Interop

ADDITIONAL RESOURCES

Using Swift with Cocoa and Objective-C (Apple)

Mike Ash Talk: Swift and C

Russ Bishop Talk: Unsafe Swift