Slide 1

Slide 1 text

Swift & Objective-C Playing together @pepibumur | www.ppinera.es

Slide 2

Slide 2 text

SugarRecord Data Storage made easy with Swift (http://github.com/SugarRecord)

Slide 3

Slide 3 text

Objective-C without the C Swift !

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Play !

Slide 6

Slide 6 text

Play ! Learn !

Slide 7

Slide 7 text

Play ! Learn ! Try !

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Swift was Designed to play together

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Bridging

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Objective-C into Swift Project-Bridging-Header.h

Slide 18

Slide 18 text

Bridging header // // Use this file to import your target's public headers that you would like to expose to Swift. // // I can import CocoaPods Libraries here! #import // And my Objective-C classes #import "CocaColaAlgorithm.h" Swift implementation // Use here your Objective-C exposed classes let cola = CocaColaAlgorithm.prepareCola()

Slide 19

Slide 19 text

Swift into Objective-C Autogenerated ProductName-Swift.h file (Not TargetName!) Swift.swift class NSObjectSwiftClass: NSObject { } ProductName-Swift.h SWIFT_CLASS("_TtC9SwiftObjc18NSObjectSwiftClass") @interface NSObjectSwiftClass : NSObject - (instancetype)init OBJC_DESIGNATED_INITIALIZER; @end

Slide 20

Slide 20 text

What's exposed? • Marked with @objc or Objective-C class descendant • public elements • private are exposed if marked with @IBAction, @IBOutlet, or @objc • internal are exposed if the project has an Objective-C bridging header • Objective-C compatible features

Slide 21

Slide 21 text

Swift only features • Generics, tuples and Swift enums • Structures defined in Swift • Top-level functions defined in Swift • Global variables defined in Swift • Typealiases defined in Swift • Swift-style variadics • Nested types and curried functions

Slide 22

Slide 22 text

No more imports! ! For .swift files in the same target

Slide 23

Slide 23 text

Be careful with circular dependencies Don't include Product-Swift.h in your Objc-C .h files

Slide 24

Slide 24 text

Use forward declaration @class

Slide 25

Slide 25 text

Naming your product Module • Non alpha-numeric characters replaced by "_" (or starting by a number) • XCode generates _fit-Swift.h from the product name 8fit

Slide 26

Slide 26 text

What can we do? And what not?

Slide 27

Slide 27 text

Inheriting Objective-C classes (Remember to use the @override keyword) class MyCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

Slide 28

Slide 28 text

You cannot subclass Swift classes in Objective-C (Even if the class is a Swift @objc or NSObject class) #if !defined(SWIFT_CLASS) # if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted) # define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA # else # define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA # endif #endif SWIFT_CLASS("_TtC9SwiftObjc9ObjcClass") @interface ObjcClass - (instancetype)init OBJC_DESIGNATED_INITIALIZER; @end

Slide 29

Slide 29 text

AnyObject is the Swift equivalent of id (But it's a protocol and its type is not know until runtime) if let fifthCharacter = myObject.characterAtIndex?(5) { println("Found \(fifthCharacter) at index 5") } You can use casting if let date = lastRefreshDate as? NSDate { println("\(date.timeIntervalSinceReferenceDate)") }

Slide 30

Slide 30 text

Objective-C nils Are implicitly unwrapped in Swift Be careful! The force awakens

Slide 31

Slide 31 text

Objective-C - (NSDate *)dueDateForProject:(Project *)project; Swift func dueDateForProject(project: Project!) -> NSDate!

Slide 32

Slide 32 text

Extension & Category But swift extension can make classes conform protocols that they didn't extension MyClass: StringLiteralConvertible { typealias ExtendedGraphemeClusterLiteralType = StringLiteralType init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { self.pattern = "\(value)" } init(extendedGraphemeClusterLiteral value: StringLiteralType) { self.pattern = value } init(stringLiteral value: StringLiteralType) { self.pattern = value } }

Slide 33

Slide 33 text

Closures & Blocks Fully compatible The only difference is that variables are mutable rather than copied, e.g:

Slide 34

Slide 34 text

Objective-C __block CustomObject *myObject = [CustomObject new]; void (^myBlock)() = ^void() { NSLog(@"%@", myObject); }; Swift let customObject: MyObject = MyObject() let myBlock: () -> () = { in println("\(customObject)") }

Slide 35

Slide 35 text

FuckingBlockSyntax.com FuckingClosureSyntax.com !

Slide 36

Slide 36 text

Comparison Objective-C isEqualTo() - Equality == - Object instance Swift == - Equality === - Object instance

Slide 37

Slide 37 text

@objc @objc(ObjcCat) class SwiftCat { @objc(initWithName:) init (name: String) { /*...*/ } } @objc is inferred if it's a NSObject subclass

Slide 38

Slide 38 text

Protocols • Swift can adopt Objective-C protocols • Objective-C can adopt Swift protocol if they are NSObjectProtocol Adopting swift protocols in objective-C /** MyProtocol.swift */ @objc protocol MyProtocol: NSObjectProtocol { // Protocol stuff }

Slide 39

Slide 39 text

Protocols in Delegate pattern It must be declared as a class type (It can only be conformed by reference types, thus having a weak reference) protocol MyDelegate : class { // methods/properties }

Slide 40

Slide 40 text

Cocoa Data Types Some Swift and Objc can be used interchangeably (Remember to import Foundation)

Slide 41

Slide 41 text

String turns into NSString let myString: NSString = "123"

Slide 42

Slide 42 text

NSLocalizedString Function into NSLocalizedString Macro

Slide 43

Slide 43 text

Int, UInt, Float, Double, Bool into NSNumber let n = 42 let m: NSNumber = n

Slide 44

Slide 44 text

[AnyObject] into NSArray If casting to NSArray objects have to be AnyObject compatible NSArray into [AnyObject] for myItem in foundationArray as [UIView] { // Do whatever you want }

Slide 45

Slide 45 text

NSDictionary into [NSObject: AnyObject] [NSObject: AnyObject] into NSDictionary Keys and values must be instances of a class or bridgeable

Slide 46

Slide 46 text

[[MyClass alloc] initWithBird:bird]; MyClass(bird: bird) Failable Initialization: init?(...)

Slide 47

Slide 47 text

CocoaPods

Slide 48

Slide 48 text

✅ Objective-C pods in Swift ⬜ Swift pods WIP

Slide 49

Slide 49 text

With the theory in mind... Moving to Swift advices ✈

Slide 50

Slide 50 text

Don't touch your Objc code base! (Unless it requires a refactor. Do it in Swift)

Slide 51

Slide 51 text

Most of libraries are in Objc And you can use them in Swift! (be patient and don't try to have everything in Swift)

Slide 52

Slide 52 text

Implement Swift on isolated features • Using Swift features internally • With @objc compatible output interface

Slide 53

Slide 53 text

Swift libraries... when there is no objc option (or you have a pure Swift project)

Slide 54

Slide 54 text

Why?

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

But overall

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Resources //MARK: - You should read let swiftTypes = "https://github.com/jbrennan/swift-tips/blob/master/swift_tips.md" let realmAndObjc = "http://realm.io/news/swift-objc-best-friends-forever" let swiftReady = "http://www.toptal.com/swift/swift-is-it-ready-for-prime-time" let swiftImprovesObjc = "http://spin.atomicobject.com/2014/06/13/swift-improves-objective-c/" //MARK: - Slides let slidesURL = "http://bit.ly/1AaKRfv" println("The slides are available here: \(slidesURL)") //MARK: - Contact let emailURL = NSURL(string: "mailto://[email protected]")! UIApplication.sharedApplication().openURL()

Slide 62

Slide 62 text

Thanks Doubts?