Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
Why do we need this?• 1.2 million apps in the iOS App Store
http://www.statista.com/statistics/276623/number-of-apps-available-in-leading-app-stores/
• All written in Objective-C
• Will need porting to Swift in the future.
• Possible Swift-only APIs in the future.
Calling object methods
• Objective-C [self presentViewController:controller animated:YES completion:nil];
• Swiftself.presentViewController(controller, animated:true, completion:nil)
Declaring Classes
Objective-C vs Swift Classes
• Objective-C Header file: ClassName.h (@interface/@end)Implementation file: ClassName.m (@implementation / @end)
• SwiftNo header fileImplementation file: ClassName.swift (class)
Objective-C Class Header• File: ClassName.h@interface ClassName { // Member variable int aValue; } // Property @property (strong) UILabel *aLabel; // Instance method - (bool) isLabelVisible; // Class method + (UILabel*) newLabelWithTitle:(NSString*)title; @end
Objective-C Class Implementation
• File: ClassName.m #import “ClassName.h” @implementation ClassName - (bool) isLabelVisible { ++aValue; return !self.aLabel.hidden; } + (UILabel*) newLabelWithTitle:(NSString*)title { UILabel *newLabel = [[UILabel alloc] init]; newLabel.text = title; return newLabel } @end
Swift Class• File: ClassName.swiftclass ClassName { // Property (optional) var aLabel: UILabel? // Instance method func isLabelVisible -> bool { return !self.aLabel.hidden } // Class method class func newLabelWithTitle(title: string) -> UILabel { var newLabel = UILabel() newLabel.text = title return newLabel } }
Objective-C Initializers (constructor)
• @implementation ClassName - (id) init { // … } - (id) initWithTitle:(NSString*)title { // … } @end
Swift Initializers (constructor)
• class ClassName { init() { // … } init(title:string) { // … } }
Objective-C Deinitializers (destructor)
• @implementation ClassName - (void) dealloc { // … } @end
Swift Deinitializers (destructor)
• class ClassName { deinit { // … } }
Object Allocation• Objective -C ClassName *aClass = [[ClassName alloc] init]; ClassName *aClass2 = [[ClassName alloc] initWithTitle:@“Title”];
• Swiftvar aClass = ClassName() var aClass2 = ClassName(title:”title”)
Objective-C Properties
Objective-C Property Getter/Setter methods
• Property@interface ClassName @property (nonatomic, strong) UILabel* labelView; @end
• Getter / Setter@implementation // Getter - (UILabel*) labelView { return _labelView; } // Setter - (void) setLabelView:(UIlabel*)aView { _labelView = aView; } @end
Calling Objective-C property getter/setter methods
• Getter UILabel *label = [aClass labelView]; UILabel *label = aClass.labelView;
• Setter[aClass setLabelView:label]; aClass.labelView = label;
synthesize / dynamic• synthesize - Tells compiler to auto-generate getter/
setter methods of an Objective-C property@synthesize labelName;
• dynamic - Tells compiler that YOU will write the getter/setter methods of an Objective-C property@dynamic labelName;
• Both optional now. Compiler will generate properties automatically.
Swift Properties
Swift Properties• class ClassName { // Stored property var aValue: int // Computed property var count: int { get { return aValue; } set(newCount) { aValue = newCount; } } }
Enumerations• Objective-C enum AlertViewType { Unknown, Ok, Cancel }
• Swiftenum AlertViewType { case Unknown case Ok case Cancel }
Objective-C Switch statements
• switch (aVal) { case 1: case 2: // do something break; case 3: // do something break; }
Swift Switch statements
• Must be exhaustive
• Do not normally fall through to the next case. Add ‘fallthrough’ keyword to enable fall through
• No break needed on each case.
• switch aVal { case 1: fallthrough case 2: // do something case 3: // do something default: // do something }
Strings
• Objective-CNSString *x = @“This is a test”;
• Swiftvar x = “This is a test”
• Just remove the “@“ symbol before the string
Mutable / Immutable objects• Objective-CNSMutableArray / NSArray NSMutableDictionary / NSDictionary NSMutableString / NSString
• Swift// Mutable var name = “John Smith” var info = [“FirstName”: “John”, “LastName”: “Smith”]var petNames = [“Spot”, “Fido”, Cerebrus”] // Immutable let name2 = “John Smith” let info2 = [“FirstName”: “John”, “LastName”: “Smith”]let petNames = [“Spot”, “Fido”, Cerebrus”]
Calling Objective-C from Swift
Objective-C bridging header• Filename: <TargetName>-Bridging-Header.h
• Auto-created by Xcode the first time you add a Swift class to a project
• Make sure you add this file to your project / source control!
• Any classes defined in this header are automatically visible to Swift code
• Manually add #include/#import lines to this file to include any Objective-C classes you want visible from Swift
• Allocate/call Objective-C classes from Swift as if they were standard Swift classes
Calling Swift from Objective-C
Marking Swift classes visible to Objective-C
• Add @objc attribute to Swift class @objc(ClassName) class ClassName { // Definition of class… }
Swift Module Header• Include in Objective-C code to include Swift classes callable
from Objective-C
• Auto generated by Xcode. File name: <ModuleName>-Swift.h
Using Swift classes in Objective-C code
• Include Swift module header#import “ModuleName-Swift.h”
• Allocate and call as if it is a regular Objective-C classClassName *aClass = [[ClassName alloc] init];[aClass someMethod:param1 width:10 height:20];
Resources
• Apple WWDC iOS App
• WWDC 2014 Videos Session 402: Introduction to Swift Session 403: Intermediate Swift Session 404: Advanced Swift Session 406: Integrating Swift with Objective-C