31
Porting Objective-C to Swift Richard Ekle [email protected]

Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Porting Objective-C to Swift

Richard Ekle [email protected]

Page 2: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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.

Page 3: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Calling object methods

• Objective-C [self presentViewController:controller animated:YES completion:nil];

• Swiftself.presentViewController(controller, animated:true, completion:nil)

Page 4: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Declaring Classes

Page 5: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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)

Page 6: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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

Page 7: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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

Page 8: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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 } }

Page 9: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Objective-C Initializers (constructor)

• @implementation ClassName - (id) init { // … } - (id) initWithTitle:(NSString*)title { // … } @end

Page 10: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Swift Initializers (constructor)

• class ClassName { init() { // … } init(title:string) { // … } }

Page 11: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Objective-C Deinitializers (destructor)

• @implementation ClassName - (void) dealloc { // … } @end

Page 12: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Swift Deinitializers (destructor)

• class ClassName { deinit { // … } }

Page 13: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Object Allocation• Objective -C ClassName *aClass = [[ClassName alloc] init]; ClassName *aClass2 = [[ClassName alloc] initWithTitle:@“Title”];

• Swiftvar aClass = ClassName() var aClass2 = ClassName(title:”title”)

Page 14: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Objective-C Properties

Page 15: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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

Page 16: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Calling Objective-C property getter/setter methods

• Getter UILabel *label = [aClass labelView]; UILabel *label = aClass.labelView;

• Setter[aClass setLabelView:label]; aClass.labelView = label;

Page 17: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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.

Page 18: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Swift Properties

Page 19: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Swift Properties• class ClassName { // Stored property var aValue: int // Computed property var count: int { get { return aValue; } set(newCount) { aValue = newCount; } } }

Page 20: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Enumerations• Objective-C enum AlertViewType { Unknown, Ok, Cancel }

• Swiftenum AlertViewType { case Unknown case Ok case Cancel }

Page 21: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Objective-C Switch statements

• switch (aVal) { case 1: case 2: // do something break; case 3: // do something break; }

Page 22: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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 }

Page 23: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Strings

• Objective-CNSString *x = @“This is a test”;

• Swiftvar x = “This is a test”

• Just remove the “@“ symbol before the string

Page 24: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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”]

Page 25: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Calling Objective-C from Swift

Page 26: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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

Page 27: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Calling Swift from Objective-C

Page 28: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

Marking Swift classes visible to Objective-C

• Add @objc attribute to Swift class @objc(ClassName) class ClassName { // Definition of class… }

Page 29: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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

Page 30: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.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];

Page 31: Porting Objective-C to Swift - Meetupfiles.meetup.com/1748372/Porting Objective-C to Swift.pdf · Objective-C bridging header • Filename: -Bridging-Header.h •

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