ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
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
OBJECTIVE-C
SWIFT CLASS MUST BE
A DESCENDANT OF AN
OBJECTIVE-C CLASS?
¡­TO BE ACCESSIBLE
IN OBJECTIVE-C FILES
Child2
Base
Child1
Class4
Class3
NSObject
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
?
MIXED PROJECTS
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).¡±
Migrating from Objective-C to Swift
Migrating from Objective-C to Swift
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 SWIFT HEADER FILE
THERE WILL LEAVE YOU WITH
A CIRCULAR REFERENCE.
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 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
SWIFT ENUM ?
IN OBJECTIVE-C
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
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 de?ne methods really used by Obj-C?
?
?
?
?
?
More about elegant API Endpoints in Alamo?re?
https://github.com/Alamo?re/Alamo?re#crud--authorization
WHAT WILL ?
NSCODER ?
FAIL TO?SEE?
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(forUnde?nedKey key: String) -> Any? and
setValue(_ value: Any?, forUnde?nedKey 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
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
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 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
speci?cally provides a domain, code, and user-info
dictionary?
?
?
More: 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 {}
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 when you use an optional ?
in string interpolation?
?
?
?
?
?
?
More: https://oleb.net/blog/2016/12/optionals-string-
interpolation
CASE STUDY: MIGRATING FROM OBJECTIVE-C TO SWIFT
APNS token == ?32bytes¡±
read more at tech.olx.com
THANK YOU

More Related Content

Similar to Migrating from Objective-C to Swift (20)

Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
Mindfire Solutions
?
Getting Swifty in an Objective-C World
Getting Swifty in an Objective-C WorldGetting Swifty in an Objective-C World
Getting Swifty in an Objective-C World
Jason Grandelli
?
21st Docker Switzerland Meetup - ISTIO
21st Docker Switzerland Meetup - ISTIO21st Docker Switzerland Meetup - ISTIO
21st Docker Switzerland Meetup - ISTIO
Niklaus Hirt
?
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
?lafur Andri Ragnarsson
?
Managing microservices with istio on OpenShift - Meetup
Managing microservices with istio on OpenShift - MeetupManaging microservices with istio on OpenShift - Meetup
Managing microservices with istio on OpenShift - Meetup
Jos¨¦ Rom¨¢n Mart¨ªn Gil
?
Shift Dev Conf API
Shift Dev Conf APIShift Dev Conf API
Shift Dev Conf API
C¨¦drick Lunven
?
Serhiy Kalinets "Building Service Mesh with .NET Core"
Serhiy Kalinets "Building Service Mesh with .NET Core"Serhiy Kalinets "Building Service Mesh with .NET Core"
Serhiy Kalinets "Building Service Mesh with .NET Core"
Fwdays
?
Framework Agnostic Discovery
Framework Agnostic DiscoveryFramework Agnostic Discovery
Framework Agnostic Discovery
KubeAcademy
?
AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024
Guido Maria Nebiolo
?
java vs C#
java vs C#java vs C#
java vs C#
Shivalik college of engineering
?
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
Sayed Ahmed
?
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
Sayed Ahmed
?
Kotlin
KotlinKotlin
Kotlin
Ademar Alves de Oliveira
?
Newspeak: Evolving Smalltalk for the Age of the Net
Newspeak: Evolving Smalltalk for the Age of the NetNewspeak: Evolving Smalltalk for the Age of the Net
Newspeak: Evolving Smalltalk for the Age of the Net
ESUG
?
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
?
Deploy Application on Kubernetes
Deploy Application on KubernetesDeploy Application on Kubernetes
Deploy Application on Kubernetes
Opsta
?
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
aptechsravan
?
C++ programing lanuage
C++ programing lanuageC++ programing lanuage
C++ programing lanuage
Nimai Chand Das
?
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#
Sagar Pednekar
?
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreter
Roberto Nogueira
?
Getting Swifty in an Objective-C World
Getting Swifty in an Objective-C WorldGetting Swifty in an Objective-C World
Getting Swifty in an Objective-C World
Jason Grandelli
?
21st Docker Switzerland Meetup - ISTIO
21st Docker Switzerland Meetup - ISTIO21st Docker Switzerland Meetup - ISTIO
21st Docker Switzerland Meetup - ISTIO
Niklaus Hirt
?
Serhiy Kalinets "Building Service Mesh with .NET Core"
Serhiy Kalinets "Building Service Mesh with .NET Core"Serhiy Kalinets "Building Service Mesh with .NET Core"
Serhiy Kalinets "Building Service Mesh with .NET Core"
Fwdays
?
Framework Agnostic Discovery
Framework Agnostic DiscoveryFramework Agnostic Discovery
Framework Agnostic Discovery
KubeAcademy
?
AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024
Guido Maria Nebiolo
?
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
Sayed Ahmed
?
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
Sayed Ahmed
?
Newspeak: Evolving Smalltalk for the Age of the Net
Newspeak: Evolving Smalltalk for the Age of the NetNewspeak: Evolving Smalltalk for the Age of the Net
Newspeak: Evolving Smalltalk for the Age of the Net
ESUG
?
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
?
Deploy Application on Kubernetes
Deploy Application on KubernetesDeploy Application on Kubernetes
Deploy Application on Kubernetes
Opsta
?
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#
Sagar Pednekar
?
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreter
Roberto Nogueira
?

Migrating from Objective-C to Swift