Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407...
Transcript of Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407...
![Page 1: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/1.jpg)
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
#WWDC14
Swift Interoperability in Depth
Session 407 Doug Gregor Engineer, Swift Compiler Team
Tools
![Page 2: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/2.jpg)
Introduction
Swift is a new language for Cocoa
Seamless interoperability with Objective-C
Focus on language-level interoperability
![Page 3: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/3.jpg)
Roadmap
Working with Cocoa • How Objective-C APIs look and feel in Swift
• id and AnyObject
Bridging Core Cocoa Types
Subclassing Objective-C Classes
CF Interoperability
![Page 4: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/4.jpg)
Working with Cocoa
![Page 5: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/5.jpg)
Swift View of Objective-C APIs
Swift provides seamless access to Objective-C APIs
![Page 6: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/6.jpg)
Swift View of Objective-C APIs
Swift provides seamless access to Objective-C APIs
Swift view of an Objective-C API is different from Objective-C
![Page 7: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/7.jpg)
Swift View of Objective-C APIs
Swift provides seamless access to Objective-C APIs
Swift view of an Objective-C API is different from Objective-C
Same Cocoa conventions and idioms
![Page 8: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/8.jpg)
Our Example: UIDocument
typedef NS_ENUM(NSInteger, UIDocumentSaveOperation) { UIDocumentSaveForCreating, UIDocumentSaveForOverwriting }; !@interface UIDocument : NSObject !@property NSDate *fileModificationDate; !- (instancetype)initWithFileURL:(NSURL *)url; !- (NSString *)fileNameExtensionForType:(NSString *)typeName saveOperation:(UIDocumentSaveOperation)saveOperation; !@end
![Page 9: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/9.jpg)
Properties
Objective-C@property NSDate *fileModificationDate;
![Page 10: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/10.jpg)
Properties
Objective-C@property NSDate *fileModificationDate;
Swiftvar fileModificationDate: NSDate!
![Page 11: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/11.jpg)
Implicitly Unwrapped Optionals
var fileModificationDate: NSDate!
![Page 12: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/12.jpg)
Implicitly Unwrapped Optionals
var fileModificationDate: NSDate!
A value of class type in Swift is never nil
![Page 13: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/13.jpg)
Implicitly Unwrapped Optionals
var fileModificationDate: NSDate!
A value of class type in Swift is never nil
Optional types generalize the notion of nil
• Intermediate Swift Presidio Wednesday 2:00PM
![Page 14: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/14.jpg)
Implicitly Unwrapped Optionals
var fileModificationDate: NSDate!
A value of class type in Swift is never nil
Optional types generalize the notion of nil
Objective-C does not have a notion of a “never-nil” pointer
• Intermediate Swift Presidio Wednesday 2:00PM
![Page 15: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/15.jpg)
Implicitly Unwrapped Optionals
var fileModificationDate: NSDate!
A value of class type in Swift is never nil
Optional types generalize the notion of nil
Objective-C does not have a notion of a “never-nil” pointer
‘!’ is an implicitly unwrapped optional • Can be tested explicitly for nil • Can directly access properties/methods of the underlying value
• Can be implicitly converted to its underlying value (e.g., NSDate)
• Intermediate Swift Presidio Wednesday 2:00PM
![Page 16: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/16.jpg)
Objective-C@property (readonly) NSString *fileType;
Mapping Objective-C Types to Swift
![Page 17: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/17.jpg)
Objective-C@property (readonly) NSString *fileType;
Swiftvar fileType: String! { get }
Mapping Objective-C Types to Swift
![Page 18: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/18.jpg)
Objective-C Types in Swift
Objective-C Type Swift Equivalent
BOOL Bool
NSInteger Int
SEL Selector
id AnyObject!
Class AnyClass!
NSString * String!
NSArray * AnyObject[]!
![Page 19: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/19.jpg)
Objective-C- (NSString *)fileNameExtensionForType:(NSString *)typeName saveOperation:(UIDocumentSaveOperation)saveOperation;
Methods
![Page 20: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/20.jpg)
Objective-C- (NSString *)fileNameExtensionForType:(NSString *)typeName saveOperation:(UIDocumentSaveOperation)saveOperation;
Swiftfunc fileNameExtensionForType(typeName: String!, saveOperation: UIDocumentSaveOperation) -> String!
Methods
![Page 21: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/21.jpg)
MethodsArgument labels
Objective-C- (NSString *)fileNameExtensionForType:(NSString *)typeName saveOperation:(UIDocumentSaveOperation)saveOperation;
Swiftfunc fileNameExtensionForType(typeName: String!, saveOperation: UIDocumentSaveOperation) -> String!
![Page 22: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/22.jpg)
MethodsArgument labels in calls
Objective-C- (NSString *)fileNameExtensionForType:(NSString *)typeName saveOperation:(UIDocumentSaveOperation)saveOperation;
Swiftfunc fileNameExtensionForType(typeName: String!, saveOperation: UIDocumentSaveOperation) -> String!
Usagelet ext = document.fileNameExtensionForType("public.presentation", saveOperation: UIDocumentSaveOperation.ForCreating)
![Page 23: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/23.jpg)
Objective-C- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler;
Methods
![Page 24: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/24.jpg)
Objective-C- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler;
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
Methods
![Page 25: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/25.jpg)
MethodsArgument labels and internal parameter names
Objective-C- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler;
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
![Page 26: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/26.jpg)
MethodsArgument labels and internal parameter names
Objective-C- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler;
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
![Page 27: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/27.jpg)
Objective-C- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler;
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
Blocks and closuresMethods
![Page 28: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/28.jpg)
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
Methods
![Page 29: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/29.jpg)
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
Trailing closure syntaxMethods
![Page 30: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/30.jpg)
Swiftfunc saveToURL(url: NSURL!, forSaveOperation saveOperation: UIDocumentSaveOperation, completionHandler: ((success: Bool) -> Void)!)
Usagedocument.saveToURL(documentURL, saveOperation: UIDocumentSaveOperation.ForCreating) { success in if success { … } else { … } }
Trailing closure syntaxMethods
![Page 31: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/31.jpg)
Initializers
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
![Page 32: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/32.jpg)
Initializers
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
Swiftinit(fileURL url: NSURL!)
![Page 33: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/33.jpg)
Initializers
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
Swiftinit(fileURL url: NSURL!)
![Page 34: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/34.jpg)
Initializers
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
Swiftinit(fileURL url: NSURL!)
![Page 35: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/35.jpg)
Initializers
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
Swiftinit(fileURL url: NSURL!)
![Page 36: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/36.jpg)
Creating Objects
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;UIDocument *document = [[UIDocument alloc] initWithFileURL:documentURL];
![Page 37: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/37.jpg)
Creating Objects
Objective-C- (instancetype)initWithFileURL:(NSURL *)url;
Swiftinit(fileURL url: NSURL!)
UIDocument *document = [[UIDocument alloc] initWithFileURL:documentURL];
let document = UIDocument(fileURL: documentURL)
![Page 38: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/38.jpg)
Factory Methods
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
![Page 39: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/39.jpg)
Factory Methods
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;UIColor *color = [UIColor colorWithRed:1 green:0.67 blue:0.04 alpha:0];
![Page 40: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/40.jpg)
Factory Methods
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
Swiftclass func colorWithRed(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor!
UIColor *color = [UIColor colorWithRed:1 green:0.67 blue:0.04 alpha:0];
![Page 41: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/41.jpg)
Factory Methods
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
Swiftclass func colorWithRed(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor!
let color = UIColor.colorWithRed(1, green: 0.67, blue: 0.04, alpha: 0)
UIColor *color = [UIColor colorWithRed:1 green:0.67 blue:0.04 alpha:0];
![Page 42: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/42.jpg)
Factory Methods as Initializers
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
Swift
UIColor *color = [UIColor colorWithRed:1 green:0.67 blue:0.04 alpha:0];
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
![Page 43: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/43.jpg)
Factory Methods as Initializers
Objective-C+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
Swiftlet color = UIColor(red: 1, green: 0.67, blue: 0.04, alpha: 0)
UIColor *color = [UIColor colorWithRed:1 green:0.67 blue:0.04 alpha:0];
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
![Page 44: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/44.jpg)
Enums
Objective-Ctypedef NS_ENUM(NSInteger, UIDocumentSaveOperation) { UIDocumentSaveForCreating, UIDocumentSaveForOverwriting};
![Page 45: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/45.jpg)
Enums
Objective-Ctypedef NS_ENUM(NSInteger, UIDocumentSaveOperation) { UIDocumentSaveForCreating, UIDocumentSaveForOverwriting};
![Page 46: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/46.jpg)
Enums
Objective-Ctypedef NS_ENUM(NSInteger, UIDocumentSaveOperation) { UIDocumentSaveForCreating, UIDocumentSaveForOverwriting};
Swiftenum UIDocumentSaveOperation : Int { case ForCreating case ForOverwriting}
![Page 47: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/47.jpg)
Usagelet ext = document.fileNameExtensionForType("public.presentation", saveOperation:
Swiftenum UIDocumentSaveOperation : Int { case ForCreating case ForOverwriting}
![Page 48: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/48.jpg)
Usagelet ext = document.fileNameExtensionForType("public.presentation", saveOperation:
Using Enums
.ForCreating)UIDocumentSaveOperation
Swiftenum UIDocumentSaveOperation : Int { case ForCreating case ForOverwriting}
![Page 49: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/49.jpg)
Usagelet ext = document.fileNameExtensionForType("public.presentation", saveOperation:
Using Enums
.ForCreating)
Swiftenum UIDocumentSaveOperation : Int { case ForCreating case ForOverwriting}
![Page 50: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/50.jpg)
Objective-C- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;
NSError
![Page 51: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/51.jpg)
Objective-C- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;
NSError
Swiftfunc contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!
![Page 52: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/52.jpg)
Swiftfunc contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!
![Page 53: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/53.jpg)
Using NSError
Swiftfunc contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!
![Page 54: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/54.jpg)
Using NSError
Swiftfunc contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!
Usagevar error: NSError? if let contents = document.contentsForType("public.presentation", error: &error) { // use the contents }
![Page 55: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/55.jpg)
Using NSError
else if let actualError = error { // handle error }
Swiftfunc contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!
Usagevar error: NSError? if let contents = document.contentsForType("public.presentation", error: &error) { // use the contents }
![Page 56: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/56.jpg)
![Page 57: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/57.jpg)
Modernizing Your Objective-C
These rules apply to all Objective-C APIs imported into Swift
Swift benefits greatly from “modern” Objective-C:
Properties
instancetype
NS_ENUM / NS_OPTIONS
NS_DESIGNATED_INITIALIZER
![Page 58: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/58.jpg)
Modernizing Your Objective-C
These rules apply to all Objective-C APIs imported into Swift
Swift benefits greatly from “modern” Objective-C:
Properties
instancetype
NS_ENUM / NS_OPTIONS
NS_DESIGNATED_INITIALIZER
![Page 59: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/59.jpg)
Modernizing Your Objective-C
These rules apply to all Objective-C APIs imported into Swift
Swift benefits greatly from “modern” Objective-C:
Properties
instancetype
NS_ENUM / NS_OPTIONS
NS_DESIGNATED_INITIALIZER
• What’s New in LLVM Marina Wednesday 9:00AM
![Page 60: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/60.jpg)
id and AnyObject
![Page 61: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/61.jpg)
id in Objective-C
Upcastsid object = [[NSURL alloc] initWithString:@"http://developer.apple.com"]; object = view.superview;
![Page 62: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/62.jpg)
id in Objective-C
Upcastsid object = [[NSURL alloc] initWithString:@"http://developer.apple.com"]; object = view.superview;
Message sends[object removeFromSuperview];
![Page 63: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/63.jpg)
id in Objective-C
Upcastsid object = [[NSURL alloc] initWithString:@"http://developer.apple.com"]; object = view.superview;
Message sends[object removeFromSuperview];
Subscriptingid date = object[@"date"];
![Page 64: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/64.jpg)
AnyObject: An Object of Any Type
Upcastsid object = [[NSURL alloc] initWithString:@"http://developer.apple.com"]; object = view.superview;
Message sends[object removeFromSuperview];
Subscriptingid date = object[@"date"];
![Page 65: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/65.jpg)
AnyObject: An Object of Any Type
Upcastsvar object: AnyObject = NSURL(string: "http://developer.apple.com") object = view.superview
Message sends[object removeFromSuperview];
Subscriptingid date = object[@"date"];
![Page 66: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/66.jpg)
AnyObject: An Object of Any Type
Upcastsvar object: AnyObject = NSURL(string: "http://developer.apple.com") object = view.superview
Message sendsobject.removeFromSuperview()
Subscriptingid date = object[@"date"];
![Page 67: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/67.jpg)
AnyObject: An Object of Any Type
Upcastsvar object: AnyObject = NSURL(string: "http://developer.apple.com") object = view.superview
Message sendsobject.removeFromSuperview()
Subscriptinglet date = object["date"]
![Page 68: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/68.jpg)
respondsToSelector Idiom
Messaging id or AnyObject can result in “unrecognized selector” failures[object removeFromSuperview];
![Page 69: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/69.jpg)
respondsToSelector Idiom
Messaging id or AnyObject can result in “unrecognized selector” failures[object removeFromSuperview];
respondsToSelector idiom to test the presence of a methodif ([object respondsToSelector:@selector(removeFromSuperview)]) { [object removeFromSuperview];}
![Page 70: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/70.jpg)
Checking the Presence of a Method
A method of AnyObject is “optional”object.removeFromSuperview()
![Page 71: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/71.jpg)
Checking the Presence of a Method
A method of AnyObject is “optional”object.removeFromSuperview
Chaining ? folds the respondsToSelector check into the call
?()
![Page 72: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/72.jpg)
Downcasting AnyObject
AnyObject does not implicitly downcastlet view: UIView = object
![Page 73: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/73.jpg)
Downcasting AnyObject
AnyObject does not implicitly downcastlet view: UIView = object // error: ‘AnyObject’ cannot be implicitly downcast
![Page 74: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/74.jpg)
Downcasting AnyObject
AnyObject does not implicitly downcastlet view: UIView = object
“as” operator forces the downcastlet view
// error: ‘AnyObject’ cannot be implicitly downcast
= object as UIView
![Page 75: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/75.jpg)
Downcasting AnyObject
AnyObject does not implicitly downcastlet view: UIView = object
“as” operator forces the downcastlet view
“as?” operator performs a conditional downcastif let view = object as? UIView { // view is a UIView}
// error: ‘AnyObject’ cannot be implicitly downcast
= object as UIView
![Page 76: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/76.jpg)
Objective-C@protocol UITableViewDataSource<NSObject>@optional- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;@end
Protocols
![Page 77: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/77.jpg)
Objective-C@protocol UITableViewDataSource<NSObject>@optional- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;@end
Swift@objc protocol UITableViewDataSource : NSObjectProtocol { func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int @optional func numberOfSectionsInTableView(tableView: UITableView) -> Int}
Protocols
![Page 78: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/78.jpg)
Objective-C@protocol UITableViewDataSource<NSObject>@optional- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;@end
Swift@objc protocol UITableViewDataSource : NSObjectProtocol { func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int @optional func numberOfSectionsInTableView(tableView: UITableView) -> Int}
Protocols
![Page 79: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/79.jpg)
Objective-C@protocol UITableViewDataSource<NSObject>@optional- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;@end
Swift@objc protocol UITableViewDataSource : NSObjectProtocol { func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int @optional func numberOfSectionsInTableView(tableView: UITableView) -> Int}
Protocols
![Page 80: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/80.jpg)
Protocol Types
Objective-C@property id <UITableViewDataSource> dataSource;
![Page 81: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/81.jpg)
Protocol Types
Objective-C@property id <UITableViewDataSource> dataSource;
Swiftvar dataSource: UITableViewDataSource!
![Page 82: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/82.jpg)
Testing Protocol Conformance
Downcast via “as?” to protocol typeif let dataSource = object as? UITableViewDataSource { let rowsInFirstSection = dataSource.tableView(tableView, numberOfRowsInSection: 0)}
![Page 83: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/83.jpg)
Protocol Types
Objective-C@property id <UITableViewDataSource> dataSource;
Swiftvar dataSource: UITableViewDataSource!
@property id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
var delegate: protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
![Page 84: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/84.jpg)
Number of Rows in the Last Section
if let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableViewlastSection = numSections - 1
(tableView)
![Page 85: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/85.jpg)
Optional Methods in Protocols
if let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableViewlastSection = numSections - 1
(tableView)
![Page 86: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/86.jpg)
Optional Methods in Protocols
if let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableViewlastSection = numSections - 1
(tableView)// error: method is optional
![Page 87: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/87.jpg)
Optional Methods in Protocols
Use the chaining ? operatorif let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableViewlastSection = numSections - 1
(tableView)
![Page 88: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/88.jpg)
Optional Methods in Protocols
Use the chaining ? operatorif let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableView if }
lastSection = numSections - 1{(tableView)?
![Page 89: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/89.jpg)
Optional Methods in Protocols
Use the chaining ? operatorif let dataSource = object as? UITableViewDataSource { var lastSection = 0
let rowsInLastSection = dataSource.tableView(tableView, numberOfRowsInSection: lastSection) }
let numSections = dataSource.numberOfSectionsInTableView if }
lastSection = numSections - 1{(tableView)?
• Advanced Swift Presidio Thursday 11:30AM
![Page 90: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/90.jpg)
Optionals and Safety
![Page 91: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/91.jpg)
Optionals and Safety
AnyObject is Swift’s equivalent to id • Similar functionality, more safe by default • AnyClass is Swift’s equivalent to Class
![Page 92: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/92.jpg)
Optionals and Safety
AnyObject is Swift’s equivalent to id • Similar functionality, more safe by default • AnyClass is Swift’s equivalent to Class
Optionals used throughout the language to represent dynamic checks • as? for safe downcasting, protocol conformance checking
• Optionals when referring to methods that may not be available • if let and chaining ? make optionals easy to use
![Page 93: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/93.jpg)
Bridging Core Cocoa Types
![Page 94: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/94.jpg)
Native Strings, Arrays, Dictionaries
One set of general-purpose native value types • Safe by default
• Predictable performance
• Typed collections support items of any type
!
Bridged to Cocoa NSString, NSArray, NSDictionary
!
![Page 95: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/95.jpg)
Native String Type
String is an efficient, Unicode-compliant string type
Flexible, efficient, high-level APIs for string manipulation
Value semantics
![Page 96: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/96.jpg)
Native String Type
String is an efficient, Unicode-compliant string type
Flexible, efficient, high-level APIs for string manipulation
Value semanticsvar s1 = "Hello"var s2 = s1 s1 += " Swift”println(s1) Hello Swift println(s2) Hello
![Page 97: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/97.jpg)
Characters
Iteration over a string produces characterslet dog = "Dog!🐶"for c in dog { // c is inferred as Character println(c)}
![Page 98: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/98.jpg)
Characters
Iteration over a string produces characterslet dog = "Dog!🐶"for c in dog { // c is inferred as Character println(c)}
Dog!🐶
![Page 99: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/99.jpg)
Characters and Code Points
Unicode characters cannot be efficiently encoded as fixed-width entities
![Page 100: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/100.jpg)
Characters and Code Points
Unicode characters cannot be efficiently encoded as fixed-width entities• Correct use of UTF-8 or UTF-16 requires deep knowledge of Unicode
![Page 101: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/101.jpg)
Characters and Code Points
Unicode characters cannot be efficiently encoded as fixed-width entities• Correct use of UTF-8 or UTF-16 requires deep knowledge of Unicode
• Low-level operations (length, characterAtIndex) are not provided by String
![Page 102: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/102.jpg)
Characters and Code Points
Unicode characters cannot be efficiently encoded as fixed-width entities• Correct use of UTF-8 or UTF-16 requires deep knowledge of Unicode
• Low-level operations (length, characterAtIndex) are not provided by String
countElements can be used to count the number of characterslet dog = "Dog!🐶" println("There are \(countElements(dog)) characters in `\(dog)’")
![Page 103: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/103.jpg)
Characters and Code Points
Unicode characters cannot be efficiently encoded as fixed-width entities• Correct use of UTF-8 or UTF-16 requires deep knowledge of Unicode
• Low-level operations (length, characterAtIndex) are not provided by String
countElements can be used to count the number of characterslet dog = "Dog!🐶" println("There are \(countElements(dog)) characters in `\(dog)’")
// There are 5 characters in `Dog!🐶’
![Page 104: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/104.jpg)
Code Points
UTF-16 is available via a propertyfor codePoint in dog.utf16 { // codePoint is inferred as UInt16 // … }print("There are \(countElements(dog.utf16)) UTF-16 code points in `\(dog)’")
![Page 105: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/105.jpg)
Code Points
UTF-16 is available via a propertyfor codePoint in dog.utf16 { // codePoint is inferred as UInt16 // … }print("There are \(countElements(dog.utf16)) UTF-16 code points in `\(dog)’")
// There are 6 UTF-16 code points in `Dog!🐶’
![Page 106: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/106.jpg)
String and NSString
Foundation NSString APIs are available on Stringlet fruits = "apple;banana;cherry".componentsSeparatedByString(";")
![Page 107: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/107.jpg)
String and NSString
Foundation NSString APIs are available on Stringlet fruits = "apple;banana;cherry".componentsSeparatedByString(";")
// inferred as String[]
![Page 108: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/108.jpg)
String and NSString
Foundation NSString APIs are available on Stringlet fruits = "apple;banana;cherry".componentsSeparatedByString(";")
Cast to NSString to access properties and methods on NSString categories("Welcome to WWDC 2014" as NSString).myNSStringMethod()
// inferred as String[]
![Page 109: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/109.jpg)
String and NSString
Foundation NSString APIs are available on Stringlet fruits = "apple;banana;cherry".componentsSeparatedByString(";")
Cast to NSString to access properties and methods on NSString categories("Welcome to WWDC 2014" as NSString).myNSStringMethod()
Extend String with your methodextension String { func myStringMethod() -> String { … }}
// inferred as String[]
![Page 110: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/110.jpg)
Objective-C@property NSArray *toolbarItems;
NSArray Bridges to Array of AnyObject
![Page 111: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/111.jpg)
Objective-C@property NSArray *toolbarItems;
Swiftvar toolbarItems: AnyObject[]!
NSArray Bridges to Array of AnyObject
![Page 112: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/112.jpg)
Upcasting Arrays
An array T[] can be assigned to an AnyObject[]let myToolbarItems: UIBarButtonItem[] = [item1, item2, item3]controller.toolbarItems = myToolbarItems
![Page 113: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/113.jpg)
Iteration over an AnyObject[] produces AnyObject valuesfor object: AnyObject in viewController.toolbarItems { let item = object as UIBarButtonItem // … }
Downcasting Arrays
![Page 114: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/114.jpg)
Iteration over an AnyObject[] produces AnyObject valuesfor object: AnyObject in viewController.toolbarItems { let item = object as UIBarButtonItem // … }
Can downcast AnyObject[] to an array of a specific typefor item in viewController.toolbarItems as UIBarButtonItem[] { // … }
Downcasting Arrays
![Page 115: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/115.jpg)
Swift array has two representations
Array Bridging—Under the Hood
T[]
![Page 116: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/116.jpg)
Swift array has two representations
Array Bridging—Under the Hood
Native0 1 2 3 4 5length capacity 6
T[]
![Page 117: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/117.jpg)
Swift array has two representations
Array Bridging—Under the Hood
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
![Page 118: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/118.jpg)
Swift array has two representations
Array methods manage the representation internally
Array Bridging—Under the Hood
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
![Page 119: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/119.jpg)
Swift array has two representations
Array methods manage the representation internally
Bridging converts between NSArray and a Swift array
Array Bridging—Under the Hood
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
![Page 120: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/120.jpg)
NSArray Swift Array Bridging
Returning an NSArray* from an Objective-C method to Swift let items: AnyObject[] = viewController.toolbarItems
→
![Page 121: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/121.jpg)
NSArray Swift Array Bridging
Returning an NSArray* from an Objective-C method to Swift let items: AnyObject[] = viewController.toolbarItems
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
→
![Page 122: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/122.jpg)
NSArray Swift Array Bridging
Returning an NSArray* from an Objective-C method to Swift let items: AnyObject[] = viewController.toolbarItems
Calls copy() to ensure the array won’t change underneath us
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
→
![Page 123: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/123.jpg)
NSArray Swift Array Bridging
Returning an NSArray* from an Objective-C method to Swift let items: AnyObject[] = viewController.toolbarItems
Calls copy() to ensure the array won’t change underneath us• For immutable NSArrays, this operation is trivial
NSArray object
Native
Cocoa
0 1 2 3 4 5length capacity 6
T[]
→
![Page 124: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/124.jpg)
Passing a Swift array to an Objective-C method expecting an NSArray*viewController.toolbarItems = myToolbarItems
T[] NSArray Bridging→
![Page 125: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/125.jpg)
isa
Passing a T[] to an Objective-C method expecting an NSArray*viewController.toolbarItems = myToolbarItems
NSArray object
Native
Cocoa
T[]
0 1 2 3 4 5length capacity 6
T[] NSArray Bridging→
![Page 126: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/126.jpg)
isa
Passing a T[] to an Objective-C method expecting an NSArray*viewController.toolbarItems = myToolbarItems
Native array representation “isa” NSArray, optimized
NSArray object
Native
Cocoa
T[]
0 1 2 3 4 5length capacity 6
T[] NSArray Bridging→
![Page 127: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/127.jpg)
Subclassing Objective-C Classes
![Page 128: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/128.jpg)
Swift Objects Are Objective-C Objects
All Swift classes are “id compatible”• Same layout as an Objective-C class
• Same basic infrastructure (retain/release/class/etc.)
![Page 129: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/129.jpg)
Swift Objects Are Objective-C Objects
All Swift classes are “id compatible”• Same layout as an Objective-C class
• Same basic infrastructure (retain/release/class/etc.)
Inherit from an Objective-C class to make your class directly visible in Objective-Cclass MyDocument : UIDocument { var items: String[] = []}
![Page 130: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/130.jpg)
Overriding Methods
“override” keyword required when overriding a methodoverride func handleError(error: NSError!, userInteractionPermitted: Bool) { // customized behavior super.handleError(error, userInteractionPermitted: userInteractionPermitted)}
![Page 131: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/131.jpg)
Overriding Properties
Override the property itself, not the getter or setteroverride var description: String { return "MyDocument containing \(items)" }
![Page 132: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/132.jpg)
Overriding and NSError**
NSErrorPointer is Swift’s version of NSError**override func contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject! { if cannotProduceContentsForType(typeName) { if error { error.memory = NSError(domain: domain, code: code, userInfo: [:]) } return nil } // … }
![Page 133: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/133.jpg)
Swiftclass MyDocument : UIDocument { var items: String[] = [] override func handleError(error: NSError, userInteractionPermitted: Bool) override var description: String override func contentsForType(typeName: String!, error: NSErrorPointer) -> AnyObject!}
Your Swift Class…
![Page 134: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/134.jpg)
Objective-CSWIFT_CLASS("_TtC5MyApp10MyDocument")@interface MyDocument : UIDocument@property (nonatomic) NSArray *items;- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;@property (nonatomic, readonly) NSString *description;- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;@end
Your Swift Class…in Objective-C
![Page 135: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/135.jpg)
Objective-CSWIFT_CLASS("_TtC5MyApp10MyDocument")@interface MyDocument : UIDocument@property (nonatomic) NSArray *items;- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;@property (nonatomic, readonly) NSString *description;- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;@end
Your Swift Class…in Objective-C
![Page 136: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/136.jpg)
Objective-CSWIFT_CLASS("_TtC5MyApp10MyDocument")@interface MyDocument : UIDocument@property (nonatomic) NSArray *items;- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;@property (nonatomic, readonly) NSString *description;- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;@end
Your Swift Class…in Objective-C
// Swift: var items: String[] = []
![Page 137: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/137.jpg)
Objective-CSWIFT_CLASS("_TtC5MyApp10MyDocument") @interface MyDocument : UIDocument@property (nonatomic) NSArray *items;- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;@property (nonatomic, readonly) NSString *description;- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;@end
Your Swift Class…in Objective-C
![Page 138: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/138.jpg)
Objective-CSWIFT_CLASS("_TtC5MyApp10MyDocument") @interface MyDocument : UIDocument@property (nonatomic) NSArray *items;- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;@property (nonatomic, readonly) NSString *description;- (id)contentsForType:(NSString *)typeName error:(NSError **)outError;@end
Your Swift Class…in Objective-C
// usually written MyApp.MyDocument
![Page 139: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/139.jpg)
Limitations of Objective-C
Swift has advanced features that aren’t expressible in Objective-C Tuples Generics Enums and structsfunc myGenericMethod<T>(x: T) -> (String, String) { … }
![Page 140: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/140.jpg)
Limitations of Objective-C
Swift has advanced features that aren’t expressible in Objective-C Tuples Generics Enums and structs
“objc” attribute verifies that the declaration can be used in Objective-C
func myGenericMethod<T>(x: T) -> (String, String) { … }@objc// error: not expressible in Objective-C
![Page 141: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/141.jpg)
Controlling Objective-C Names
“objc” attribute can be used to change the name of an Objective-C methodvar enabled: Bool { set { … }}
get { … }
![Page 142: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/142.jpg)
Controlling Objective-C Names
“objc” attribute can be used to change the name of an Objective-C methodvar enabled: Bool { set { … }}
// property is named “enabled”// getter is named “// setter is named “setEnabled:”
get { … } enabled”
![Page 143: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/143.jpg)
Controlling Objective-C Names
“objc” attribute can be used to change the name of an Objective-C methodvar enabled: Bool { set { … }}
// property is named “enabled”// getter is named “// setter is named “setEnabled:”
@objc(isEnabled) get { … } isEnabled”
![Page 144: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/144.jpg)
Controlling Objective-C Names
“objc” attribute can be used to change the name of an Objective-C methodvar enabled: Bool { set { … }}
Or the name of a class@objc(ABCMyDocument) class MyDocument : UIDocument { // … }
// property is named “enabled”// getter is named “// setter is named “setEnabled:”
@objc(isEnabled) get { … } isEnabled”
![Page 145: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/145.jpg)
CF Interoperability
![Page 146: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/146.jpg)
CF in Objective-C
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) {
}
![Page 147: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/147.jpg)
CF in Objective-C
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; CGFloat locations[2] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
}
![Page 148: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/148.jpg)
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; CGFloat locations[2] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
}
CF in Objective-CBridge casts
![Page 149: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/149.jpg)
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; CGFloat locations[2] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
}
CF in Objective-CThree kinds of arrays
![Page 150: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/150.jpg)
CF in Objective-C
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; CGFloat locations[2] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations); ! CGPoint startPoint = CGPointMake(width / 2, 0); CGPoint endPoint = CGPointMake(width / 2, height); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); !
}
![Page 151: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/151.jpg)
void drawGradientRect(CGContextRef context, CGColorRef startColor, CGColorRef endColor, CGFloat width, CGFloat height) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; CGFloat locations[2] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations); ! CGPoint startPoint = CGPointMake(width / 2, 0); CGPoint endPoint = CGPointMake(width / 2, height); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); ! CGColorSpaceRelease(colorSpace); CGGradientRelease(gradient); }
CF in Objective-CManual memory management
![Page 152: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/152.jpg)
CF in Swift
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { }
![Page 153: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/153.jpg)
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB()}
Managed CF Objects
![Page 154: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/154.jpg)
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB()}
Managed CF Objects
// inferred as CGColorSpace
![Page 155: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/155.jpg)
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB()}
Managed CF Objects
// colorSpace automatically released
// inferred as CGColorSpace
![Page 156: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/156.jpg)
Toll-Free Bridging Conversions
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB() let gradient = CGGradientCreateWithColors(colorSpace, [startColor, endColor], [0.0, 1.0]) }
![Page 157: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/157.jpg)
C Interoperability
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB() let gradient = CGGradientCreateWithColors(colorSpace, [startColor, endColor], [0.0, 1.0]) }
![Page 158: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/158.jpg)
Construction of C Structs
func drawGradientRect(context: CGContext, startColor: CGColor, endColor: CGColor, width: CGFloat, height: CGFloat) { let colorSpace = CGColorSpaceCreateDeviceRGB() let gradient = CGGradientCreateWithColors(colorSpace, [startColor, endColor], [0.0, 1.0]) let startPoint = CGPoint(x: width / 2, y: 0) let endPoint = CGPoint(x: width / 2, y: height) CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0)}
![Page 159: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/159.jpg)
Explicitly Bridged APIs
Some CF APIs have not been audited for implicit bridgingCGColorRef CGColorGetRandomColor(void);
![Page 160: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/160.jpg)
Explicitly Bridged APIs
Some CF APIs have not been audited for implicit bridgingCGColorRef CGColorGetRandomColor(void);
Swift uses Unmanaged<T> when the ownership convention is unknownfunc CGColorGetRandomColor() -> Unmanaged<CGColor>
![Page 161: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/161.jpg)
Working with Unmanaged Objects
Unmanaged<T> enables manual memory managementstruct Unmanaged<T: AnyObject> { func takeUnretainedValue() -> T // for +0 returns func takeRetainedValue() -> T // for +1 returns}
![Page 162: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/162.jpg)
Working with Unmanaged Objects
Unmanaged<T> enables manual memory managementstruct Unmanaged<T: AnyObject> { func takeUnretainedValue() -> T // for +0 returns func takeRetainedValue() -> T // for +1 returns}
• Advanced Swift Presidio Thursday 11:30AM
![Page 163: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/163.jpg)
Working with Unmanaged Objects
Unmanaged<T> enables manual memory managementstruct Unmanaged<T: AnyObject> { func takeUnretainedValue() -> T // for +0 returns func takeRetainedValue() -> T // for +1 returns}
Use it to work with unaudited CF APIslet color = CGColorGetRandomColor().takeUnretainedValue()
• Advanced Swift Presidio Thursday 11:30AM
![Page 164: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/164.jpg)
Working with Unmanaged Objects
Unmanaged<T> enables manual memory managementstruct Unmanaged<T: AnyObject> { func takeUnretainedValue() -> T // for +0 returns func takeRetainedValue() -> T // for +1 returns}
Use it to work with unaudited CF APIslet color = CGColorGetRandomColor().takeUnretainedValue()
// inferred as CGColor
• Advanced Swift Presidio Thursday 11:30AM
![Page 165: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/165.jpg)
Working with Unmanaged Objects
Unmanaged<T> enables manual memory managementstruct Unmanaged<T: AnyObject> { func takeUnretainedValue() -> T // for +0 returns func takeRetainedValue() -> T // for +1 returns}
Use it to work with unaudited CF APIslet color = CGColorGetRandomColor().takeUnretainedValue()
func retain() -> Unmanaged<T>func release()func autorelease() -> Unmanaged<T>
// inferred as CGColor
• Advanced Swift Presidio Thursday 11:30AM
![Page 166: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/166.jpg)
Implicit Bridging
Audit CF APIs to ensure that conform to CF memory conventionsCGColorRef CGColorGetRandomColor(void);
Swift uses Unmanaged<T> when the ownership convention is unknownfunc CGColorGetRandomColor() -> Unmanaged<CGColor>
![Page 167: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/167.jpg)
Implicit Bridging
Audit CF APIs to ensure that conform to CF memory conventionsCF_IMPLICIT_BRIDGING_ENABLEDCGColorRef CGColorGetRandomColor(void);CF_IMPLICIT_BRIDGING_DISABLED
Swift uses Unmanaged<T> when the ownership convention is unknownfunc CGColorGetRandomColor() -> Unmanaged<CGColor>
![Page 168: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/168.jpg)
Implicit Bridging
Audit CF APIs to ensure that conform to CF memory conventionsCF_IMPLICIT_BRIDGING_ENABLEDCGColorRef CGColorGetRandomColor(void);CF_IMPLICIT_BRIDGING_DISABLED
Implicit bridging eliminates Unmanaged<T>func CGColorGetRandomColor() -> CGColor
![Page 169: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/169.jpg)
Summary
Seamless interoperability between Swift and Objective-C • Let the tools help you understand the relationship
Bridging of Core Cocoa types • Prefer native strings, arrays, dictionaries
Automated CF memory management
![Page 170: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/170.jpg)
More Information
Dave DeLong Developer Tools Evangelist [email protected]
Documentation Using Swift with Cocoa and Objective-C http://apple.com
Apple Developer Forums http://devforums.apple.com
![Page 171: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/171.jpg)
Related Sessions
• Integrating Swift with Objective-C Presidio Wednesday 9:00AM
• Intermediate Swift Presidio Wednesday 2:00PM
• Advanced Swift Presidio Thursday 11:30AM
![Page 172: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/172.jpg)
Labs
• Swift Tools Lab A Thursday 9:00AM
• Swift Tools Lab A Thursday 2:00PM
• Swift Tools Lab A Friday 9:00AM
• Swift Tools Lab A Friday 2:00PM
![Page 173: Swift Interoperability in Depth - Apple Inc.€¦ · Swift Interoperability in Depth Session 407 Doug Gregor Engineer, Swift Compiler Team Tools. Introduction Swift is a new language](https://reader035.fdocuments.in/reader035/viewer/2022070712/5ecedf96306e27216e533490/html5/thumbnails/173.jpg)