Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CASE STUDY: MIGRATING
FROM OBJECTIVE-C TO SWIFT
DOMINIQUE STRANZ
MIGRATE 

TO SWIFT
STAY WITH
OBJECTIVE-C
?
WE DECIDED TO
MIGRATE 🎉
CUI BONO?
Lucius Cassius Longinus
DEVELOPERS
STRING
INTERPOLATION
OPTIONALS
TUPLES
STRING ENUMS
NO MORE 

HEADER FILES
PROTOCOLS

EXTENSIONS
and so on, 

and so forth…
HOW DO

I START?
Application
Configuration

Manager
API Client
Utils
Application
Configuration

Manager
new API Client
Utils
Application
new API Client
Utils
Adopt Nullability
Configuration

Manager
Adopt Nullability
Configuration

Manager
Adopt Nullability
Configuration

Manager
Adopt Nullability
Configuration

Manager
Application
new API Client
Utils
Adopt Nullability
Configuration

Manager
ALL NEW
FEATURES
WRITTEN 

IN SWIFT
USE „TECH DAYS”
TO MIGRATE 

OBJECTIVE-C
CODE 

34%
of code in
after 2 months
53%
of code in
now
3 RULES OF
EFFECTIVE
MIGRATION
FILE BY FILE
MIGRATION 

IS THE MOST
EFFECTIVE WAYChild2
Base
Child1
Class2
Class4
Class3
START WITH 

FILES WITHOUT
SUBCLASSES
Child2
Base
Child1
Class2
Class4
Class3
YOU CANNOT SUBCLASS
A SWIFT CLASS IN
OBJECTI...
SWIFT CLASS MUST BE
A DESCENDANT OF AN
OBJECTIVE-C CLASS

…TO BE ACCESSIBLE
IN OBJECTIVE-C FILES
Child2
Base
Child1
Class4...
SWIFT CLASS MUST BE
A DESCENDANT OF AN
OBJECTIVE-C CLASS

Child2
Base
Child1
Class4
Class3
NSObject
OBJECTIVE-C WILL NOT B...


MIXED PROJECTS
Mix-and-match functionality makes it easy to choose
which features and functionality to implement in
Swift, and which to l...
BUNDLE SIZE
22 MB 38 MB
YES FOR THE WATCHOS APP
NO FOR THE WATCHOS APP EXTENSION
Always Embed Swift Standard Libraries
⌚
CODE AUTOCOMPLETION
FIXED IN XCODE 8.3
IMPORTS
IN OBJECTIVE-C
HEADER FILES ONLY
USE FORWARD
DECLARATIONS
NSObject
@class ClassName;
@protocol ProtocolName;
IMPORTING A S...
IF BOTH SWIFT AND
OBJECTIVE-C CLASSES
ARE IN THE SAME
APPLICATION PROJECT
NSObject
#import "ModuleName-Swift.h"
IF BOTH SWIFT AND
OBJECTIVE-C CLASSES
ARE IN THE SAME
COCOA POD PROJECT
NSObject
#import <PodName/PodName-Swift.h>
IF SWIFT FILE IS 

IN ANOTHER POD
NSObject
@import PodName;
IF SWIFT FILE IS 

IN ANOTHER POD
NSObject
@import PodName;
ADD *.SWIFT FILES TO
SOURCE_FILES DECLARATION 

IN PODSPEC FILE
SWIFT CLASS 

IN OBJECTIVE-C
MUST BE A DESCENDANT 

OF AN OBJECTIVE-C CLASS
WILL NOT BE VISIBLE

IN OBJECTIVE-C
INT?, INT!, FLOAT?, …
SOME NAMES ARE AL...
SWIFT ENUM 

IN OBJECTIVE-C
ENUM SHOULD HAVE THE
@OBJC PREFIX
@objc prefix
ASSOCIATED VALUES 

ARE NOT SUPPORTED
case variant1(value: Int)
IN OBJECTIV...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
RawRepresentable enum for ObjectMapper?
▸ Model should provide wrapper for...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
API Endpoints as URLRequestConvertible enums?
▸ Create a proxy class (inhe...
WHAT WILL 

NSCODER 

FAIL TO SEE?
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
NSCoding
▸ NSKeyedArchiver, NSKeyedUnarchiver will see only
properties vis...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
NSCoding
▸ NSKeyedArchiver, NSKeyedUnarchiver will see only
properties vis...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
NSCoding
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
Future? Swift Archival & Serialization proposal
▸ It aims to provide a sol...
USING 

ENUMERATIONS
AS ERRORS
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 ...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
▸ Error type in Obj-C code? 

NSError without ...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
enum ServiceError: Error {}
LESSONS
LEARNED
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 ...
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
APNS token == „32bytes”
read more at tech.olx.com
THANK YOU
Migrating from Objective-C to Swift
Migrating from Objective-C to Swift
Upcoming SlideShare
Loading in …5
×

Migrating from Objective-C to Swift

364 views

Published on

Migration process of mature iOS project from Objective-C to Swift based on OLX application. Good practices how to migrate project alongside with normal product development. At the end - about problems with mixed Objective-C and Swift projects with possible solutions.

Published in: Mobile
  • Be the first to comment

  • Be the first to like this

Migrating from Objective-C to Swift

  1. 1. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT DOMINIQUE STRANZ
  2. 2. MIGRATE 
 TO SWIFT STAY WITH OBJECTIVE-C ?
  3. 3. WE DECIDED TO MIGRATE 🎉
  4. 4. CUI BONO? Lucius Cassius Longinus
  5. 5. DEVELOPERS
  6. 6. STRING INTERPOLATION
  7. 7. OPTIONALS
  8. 8. TUPLES
  9. 9. STRING ENUMS
  10. 10. NO MORE 
 HEADER FILES
  11. 11. PROTOCOLS
 EXTENSIONS
  12. 12. and so on, 
 and so forth…
  13. 13. HOW DO
 I START?
  14. 14. Application Configuration
 Manager API Client Utils
  15. 15. Application Configuration
 Manager new API Client Utils
  16. 16. Application new API Client Utils Adopt Nullability Configuration
 Manager
  17. 17. Adopt Nullability Configuration
 Manager
  18. 18. Adopt Nullability Configuration
 Manager
  19. 19. Adopt Nullability Configuration
 Manager
  20. 20. Application new API Client Utils Adopt Nullability Configuration
 Manager
  21. 21. ALL NEW FEATURES WRITTEN 
 IN SWIFT
  22. 22. USE „TECH DAYS” TO MIGRATE 
 OBJECTIVE-C CODE 

  23. 23. 34% of code in after 2 months
  24. 24. 53% of code in now
  25. 25. 3 RULES OF EFFECTIVE MIGRATION
  26. 26. FILE BY FILE MIGRATION 
 IS THE MOST EFFECTIVE WAYChild2 Base Child1 Class2 Class4 Class3
  27. 27. START WITH 
 FILES WITHOUT SUBCLASSES Child2 Base Child1 Class2 Class4 Class3 YOU CANNOT SUBCLASS A SWIFT CLASS IN OBJECTIVE-C
  28. 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. 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
  30. 30. 
 MIXED PROJECTS
  31. 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. 32. BUNDLE SIZE 22 MB 38 MB YES FOR THE WATCHOS APP NO FOR THE WATCHOS APP EXTENSION Always Embed Swift Standard Libraries ⌚
  33. 33. CODE AUTOCOMPLETION FIXED IN XCODE 8.3
  34. 34. IMPORTS
  35. 35. 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.
  36. 36. IF BOTH SWIFT AND OBJECTIVE-C CLASSES ARE IN THE SAME APPLICATION PROJECT NSObject #import "ModuleName-Swift.h"
  37. 37. IF BOTH SWIFT AND OBJECTIVE-C CLASSES ARE IN THE SAME COCOA POD PROJECT NSObject #import <PodName/PodName-Swift.h>
  38. 38. IF SWIFT FILE IS 
 IN ANOTHER POD NSObject @import PodName;
  39. 39. IF SWIFT FILE IS 
 IN ANOTHER POD NSObject @import PodName; ADD *.SWIFT FILES TO SOURCE_FILES DECLARATION 
 IN PODSPEC FILE
  40. 40. SWIFT CLASS 
 IN OBJECTIVE-C
  41. 41. 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
  42. 42. SWIFT ENUM 
 IN OBJECTIVE-C
  43. 43. 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
  44. 44. 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`
  45. 45. 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
  46. 46. WHAT WILL 
 NSCODER 
 FAIL TO SEE?
  47. 47. 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…
  48. 48. 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
  49. 49. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT NSCoding
  50. 50. 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
  51. 51. USING 
 ENUMERATIONS AS ERRORS
  52. 52. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT enum ServiceError: Error {}
  53. 53. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT enum ServiceError: Error {} ▸ Error type in Obj-C code? 
 NSError without associated values…
  54. 54. 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
  55. 55. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT enum ServiceError: Error {}
  56. 56. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT enum ServiceError: Error {}
  57. 57. LESSONS LEARNED
  58. 58. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT Optional(„2000 PLN”)
  59. 59. 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
  60. 60. CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT APNS token == „32bytes”
  61. 61. read more at tech.olx.com
  62. 62. THANK YOU

×