This document discusses lessons learned from migrating an application from Objective-C to Swift. It covers various challenges such as interoperability between the two languages, handling optionals and enums, and migrating Objective-C code and patterns to Swift equivalents. Recommendations are provided such as starting the migration with files without subclasses, using Swift features selectively, and adopting a file-by-file migration approach. Potential issues with architectures like MVC and patterns like delegation are also addressed.
27. START WITH
FILES WITHOUT
SUBCLASSES
Child2
Base
Child1
Class2
Class4
Class3
YOU CANNOT SUBCLASS
A SWIFT CLASS IN
OBJECTIVE-C
28. SWIFT CLASS MUST BE
A DESCENDANT OF AN
OBJECTIVE-C CLASS
…TO BE ACCESSIBLE
IN OBJECTIVE-C FILES
Child2
Base
Child1
Class4
Class3
NSObject
29. SWIFT CLASS MUST BE
A DESCENDANT OF AN
OBJECTIVE-C CLASS
Child2
Base
Child1
Class4
Class3
NSObject
OBJECTIVE-C WILL NOT BE
ABLE TO TRANSLATE CERTAIN
SWIFT-SPECIFIC FEATURES
31. 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).”
32.
33.
34. BUNDLE SIZE
22 MB 38 MB
YES FOR THE WATCHOS APP
NO FOR THE WATCHOS APP EXTENSION
Always Embed Swift Standard Libraries
⌚
37. IN OBJECTIVE-C
HEADER FILES ONLY
USE FORWARD
DECLARATIONS
NSObject
@class ClassName;
@protocol ProtocolName;
IMPORTING A SWIFT HEADER FILE
THERE WILL LEAVE YOU WITH
A CIRCULAR REFERENCE.
38. IF BOTH SWIFT AND
OBJECTIVE-C CLASSES
ARE IN THE SAME
APPLICATION PROJECT
NSObject
#import "ModuleName-Swift.h"
39. IF BOTH SWIFT AND
OBJECTIVE-C CLASSES
ARE IN THE SAME
COCOA POD PROJECT
NSObject
#import <PodName/PodName-Swift.h>
40. IF SWIFT FILE IS
IN ANOTHER POD
NSObject
@import PodName;
41. IF SWIFT FILE IS
IN ANOTHER POD
NSObject
@import PodName;
ADD *.SWIFT FILES TO
SOURCE_FILES DECLARATION
IN PODSPEC FILE
43. MUST BE A DESCENDANT
OF AN OBJECTIVE-C CLASS
WILL NOT BE VISIBLE
IN OBJECTIVE-C
INT?, INT!, FLOAT?, …
SOME NAMES ARE ALREADY IN
USE IN NSOBJECT
description: String
SWIFT CLASSES CANNOT BE
SUBCLASSED IN OBJECTIVE-C
objc_subclassing_restricted
METHODS WITH NIL
TERMINATION WILL BE NOT
ACCESSIBLE IN SWIFT
NS_REQUIRES_NIL_TERMINATION
DEALLOC IS UNAVAILABLE
IN SWIFT, BUT YOU CAN USE
DEINIT INSTEAD
dealloc != deinit
45. ENUM SHOULD HAVE THE
@OBJC PREFIX
@objc prefix
ASSOCIATED VALUES
ARE NOT SUPPORTED
case variant1(value: Int)
IN OBJECTIVE-C ENUM CAN
ONLY HAVE INTEGER TYPE
@enum Name: Int
46. 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`
47. 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 Alamofire
https://github.com/Alamofire/Alamofire#crud--authorization
49. 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…
50. 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
52. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
Future? Swift Archival & Serialization proposal
▸ It aims to provide a solution for the archival of Swift struct
and enum types
▸ It aims to provide a more type-safe solution for serializing
to external formats, such as JSON and plist
▸ Read more: https://github.com/itaiferber/swift-evolution/
blob/swift-archival-serialization/proposals/XXXX-swift-
archival-serialization.md
55. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
▸ Error type in Obj-C code?
NSError without associated values…
56. 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
dictionary
More: https://github.com/apple/swift-evolution/blob/
master/proposals/0112-nserror-bridging.md
61. 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 interpolation
More: https://oleb.net/blog/2016/12/optionals-string-
interpolation