CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT DOMINIQUE STRANZ
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
DOMINIQUE STRANZ
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
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.
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
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 😥
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
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
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/
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
Swift enum in Obj-C
▸ Integer raw type only
▸ Without associated values
▸ Remember about @objc prefix ⚠
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`
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
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…
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
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
NSCoding
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
▸ Error type in Obj-C code? NSError without associated values…
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
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
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
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
Optional(„2000 PLN”)
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
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
APNS token == „32bytes”
THANK YOU