Migrating from Objective-c to swift

26
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT DOMINIQUE STRANZ

Transcript of Migrating from Objective-c to swift

Page 1: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

DOMINIQUE STRANZ

Page 2: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

How to start?

▸ Adopt Nullability in Obj-C code that will be not migrated in first place - in OLX it was „Configurations Manager” pod

▸ Decide that all new features should be written in Swift

▸ Use „tech days” to refactor Obj-C code file by file to Swift

34% of code inafter 2 months

Page 3: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Tips and Reminders

▸ The most effective approach is migration file by file

▸ Start migration from files without subclasses in Obj-C (you cannot subclass a Swift class in Objective-c)

▸ Swift class must be a descendant of an Obj-C class to be accessible and usable in Obj-C (for example NSObject)

▸ Remember that Objective-C won’t be able to translate certain features that are specific to Swift.

Page 4: Migrating from Objective-c to swift

Mix-and-match functionality makes it easy to choose which features and functionality to implement in Swift, and which to leave in Objective-C. Interoperability makes it possible to integrate those features back into Objective-C code with no hassle.

Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 3).”

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Page 5: Migrating from Objective-c to swift
Page 6: Migrating from Objective-c to swift
Page 7: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Swift class in Obj-C

▸ Remember about descending from Obj-C class ⚠

▸ var id: Int!, var distance: Int? - (implicitly unwrapped) optionals mapped to primitive types in Obj-C will be not visible

▸ var description - conflicts with description from NSObject

▸ Emojis are not allowed in names 😥

Page 8: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Obj-C class in Swift

▸ You cannot subclass a Swift class in Obj-C ⚠

▸ Methods with variable number of arguments (with NS_REQUIRES_NIL_TERMINATION) will be not visible in Swift

Page 9: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

#selector in protocol extension

▸ Argument of '#selector' refers to a method that is not exposed to Objective-C. //FIX: Add @objc prefix

▸ @objc can only by used with members of classes, @objc protocols, and concrete extensions of classes//FIX: Delete @objc prefix

Page 10: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Stop @objc from making everything horrible

▸ Property cannot be a member of an @objc protocol because its type cannot be presented in Objective-C

▸ Use @objc only if it’s really necessary

▸ Extract protocol with only @objc methods to leave Swift code untouched More: http://www.jessesquires.com/avoiding-objc-in-swift/

Page 11: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Swift enum in Obj-C

▸ Integer raw type only

▸ Without associated values

▸ Remember about @objc prefix ⚠

Page 12: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

RawRepresentable enum for ObjectMapper?

▸ Model should provide wrapper for Obj-C code:

▸ Translation to enum: Int during mapping transformation or in runtime

▸ Computed property `isActive: Bool`, `is…: Bool`

Page 13: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

API Endpoints as URLRequestConvertible enums?

▸ Create a proxy class (inherited from NSObject) with class requests methods, that inside will use Swift enum

▸ We decided to only define methods really used by Obj-C More about elegant API Endpoints in Alamofirehttps://github.com/Alamofire/Alamofire#crud--authorization

Page 14: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

NSCoding

▸ NSKeyedArchiver, NSKeyedUnarchiver will see only properties visible for Objective-C

▸ We can change properties type or…

Page 15: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

NSCoding

▸ NSKeyedArchiver, NSKeyedUnarchiver will see only properties visible for Objective-C

▸ We can change properties type or…

▸ Use value(forUndefinedKey key: String) -> Any? and setValue(_ value: Any?, forUndefinedKey key: String)to set/get not bridged properties

▸ Example: Use enum’s rawValue as encodable value

Page 16: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

NSCoding

Page 17: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

enum ServiceError: Error {}

Page 18: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

enum ServiceError: Error {}

▸ Error type in Obj-C code? NSError without associated values…

Page 19: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

enum ServiceError: Error {}

▸ Error type in Obj-C code? NSError without associated values…

▸ CustomNSError protocol: Describes an error type that specifically provides a domain, code, and user-info dictionaryMore: https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md

Page 20: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

enum ServiceError: Error {}

Page 21: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

enum ServiceError: Error {}

Page 22: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Pod with mixed Obj-C and Swift?

▸ Add *.swift files to source_files in Podspec

▸ Use `#import <podname/podname-Swift.h>` in *.m files ❗#import podname-Swift.h❗@import podname

▸ Use @class in *.h files

▸ Outside module `@import podname` will import both Swift and Obj-C code

Page 23: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Optional(„2000 PLN”)

Page 24: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

Optional(„2000 PLN”)

▸ Swift 3.1 will actually actually raise a warning when you use an optional in string interpolationMore: https://oleb.net/blog/2016/12/optionals-string-interpolation

Page 25: Migrating from Objective-c to swift

CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT

APNS token == „32bytes”

Page 26: Migrating from Objective-c to swift

THANK YOU