Bringing Swift into your Objective-C Projects

325
Bringing Swift into your Objective-C Projects

Transcript of Bringing Swift into your Objective-C Projects

Page 1: Bringing Swift into your Objective-C Projects

Bringing Swift into your

Objective-C Projects

Page 2: Bringing Swift into your Objective-C Projects

Before we go any further…

Page 3: Bringing Swift into your Objective-C Projects

Hold the tomatoes, I don’t hate Objective-C

Page 4: Bringing Swift into your Objective-C Projects

Why does this matter?

Page 5: Bringing Swift into your Objective-C Projects

Apple is all in

Page 6: Bringing Swift into your Objective-C Projects

Learning Curve

Page 7: Bringing Swift into your Objective-C Projects

Better Apps

Page 8: Bringing Swift into your Objective-C Projects

Not going to discuss

• Swift Syntax

• Value vs Reference types

• How to convince your team

Page 9: Bringing Swift into your Objective-C Projects

Sit Back &

Relax

Page 10: Bringing Swift into your Objective-C Projects

Let’s Dive In

Page 11: Bringing Swift into your Objective-C Projects

The Journey

Page 12: Bringing Swift into your Objective-C Projects

Rewind to June 2014

Page 13: Bringing Swift into your Objective-C Projects

Still Learning iOS 7

Page 14: Bringing Swift into your Objective-C Projects

it’s that time of the year

Page 15: Bringing Swift into your Objective-C Projects
Page 16: Bringing Swift into your Objective-C Projects

no golden ticket

Page 17: Bringing Swift into your Objective-C Projects

I was all ears

Page 18: Bringing Swift into your Objective-C Projects

drinking mimosas with friends, watching

Page 19: Bringing Swift into your Objective-C Projects
Page 20: Bringing Swift into your Objective-C Projects

and then, all of the sudden

Page 21: Bringing Swift into your Objective-C Projects

Hair Force One gets on stage and…

Page 22: Bringing Swift into your Objective-C Projects
Page 23: Bringing Swift into your Objective-C Projects
Page 24: Bringing Swift into your Objective-C Projects
Page 25: Bringing Swift into your Objective-C Projects

And so the journey began…

Page 26: Bringing Swift into your Objective-C Projects

started studying & practicing,

Page 27: Bringing Swift into your Objective-C Projects

used Swift for anything I could think of,

Page 28: Bringing Swift into your Objective-C Projects

wrote some tutorials,

Page 29: Bringing Swift into your Objective-C Projects

taught classes at work,

Page 30: Bringing Swift into your Objective-C Projects

and tech reviewed a couple of books.

Page 31: Bringing Swift into your Objective-C Projects

I was ready

Page 32: Bringing Swift into your Objective-C Projects

Two Big Ideas

Page 33: Bringing Swift into your Objective-C Projects

Two Big Ideas

1. Learn by Doing, but not doing too much

2. Maximize Benefits

Page 34: Bringing Swift into your Objective-C Projects

Learn by Doing, but not doing too much

Big Idea 1

Page 35: Bringing Swift into your Objective-C Projects

{1} Jump

{2} One View Controller

{3} Everything above view controller

Learn

Page 36: Bringing Swift into your Objective-C Projects

{1} Jump

{2} One View Controller

{3} Everything above view controller

{1} Jump

{2} One View Controller

{3} Everything above view controller

Learn

Page 37: Bringing Swift into your Objective-C Projects

{1} Jump

Page 38: Bringing Swift into your Objective-C Projects

Back at work…

Page 39: Bringing Swift into your Objective-C Projects

inherited Objective-C project

Page 40: Bringing Swift into your Objective-C Projects

and we thought, should we start using Swift?

Page 41: Bringing Swift into your Objective-C Projects

Fellowship Formed

Page 42: Bringing Swift into your Objective-C Projects

took the plunge

Page 43: Bringing Swift into your Objective-C Projects

Why on Earth would I want to start writing Swift

when I have deadlines?

Page 44: Bringing Swift into your Objective-C Projects

Why Swift

• Faster & More Stable Apps

• Less Typing, I’m More Efficient

• It’s the future

Page 45: Bringing Swift into your Objective-C Projects

So where to start?(Architecturally)

Page 46: Bringing Swift into your Objective-C Projects

User Interface

UIApplication

UIApplicationDelegate

Core Logic

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewController

UIView

UIWindow

UIViewController

UIView

Page 47: Bringing Swift into your Objective-C Projects

Started at the top…

now we’re here

Page 48: Bringing Swift into your Objective-C Projects

{1} Jump

{2} One View Controller

{3} Everything above view controller

{1} Jump

{2} One View Controller

{3} Everything above view controller

Learn

Page 49: Bringing Swift into your Objective-C Projects

{2} One View Controller

Page 50: Bringing Swift into your Objective-C Projects

when we talk view controller it includes

the root view

Page 51: Bringing Swift into your Objective-C Projects

Swift

Objective-C

Page 52: Bringing Swift into your Objective-C Projects

User Interface

UIApplication

UIApplicationDelegate

Core Logic

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewController

UIView

UIWindow

UIViewController

UIView

Page 53: Bringing Swift into your Objective-C Projects

User Interface

UIApplication

UIApplicationDelegate

Core Logic

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewControllerUIViewController

UIView UIView

UIWindow

UIViewController

UIView

Page 54: Bringing Swift into your Objective-C Projects

Why did we start here?

Page 55: Bringing Swift into your Objective-C Projects

Why did we start here?

• Minimize Risk

• Nice Set-up

• It’s Clean

Page 56: Bringing Swift into your Objective-C Projects

Minimize Risk

Page 57: Bringing Swift into your Objective-C Projects

Minimize Risk

• Don’t have to learn the whole language

• Easy U-turn

• Small and contained

Page 58: Bringing Swift into your Objective-C Projects

Nice Set-up

Page 59: Bringing Swift into your Objective-C Projects

Nice Set-up

• Flow Swift safety up into view controllers

• Setup for actually improving app

• Critical for re-writes

Page 60: Bringing Swift into your Objective-C Projects

It’s Clean

Page 61: Bringing Swift into your Objective-C Projects

It’s Clean

• One single file

• Easy to coordinate within team

• Don’t have to worry about things ObjC can’t see

Page 62: Bringing Swift into your Objective-C Projects

Ok, so started building my first Swifty view controller

Page 63: Bringing Swift into your Objective-C Projects

first step…

Page 64: Bringing Swift into your Objective-C Projects
Page 65: Bringing Swift into your Objective-C Projects
Page 66: Bringing Swift into your Objective-C Projects
Page 67: Bringing Swift into your Objective-C Projects
Page 68: Bringing Swift into your Objective-C Projects

you’ll need to import Objective-C headers in

the bridging header

Page 69: Bringing Swift into your Objective-C Projects

got my .swift view controller ready to build

Page 70: Bringing Swift into your Objective-C Projects

time to build

Page 71: Bringing Swift into your Objective-C Projects

started writing some autolayout code

using Masonry, and…

Page 72: Bringing Swift into your Objective-C Projects

Misty Mountains

Page 73: Bringing Swift into your Objective-C Projects

Swift does not support macros

Page 74: Bringing Swift into your Objective-C Projects

Masonry uses macros

Page 75: Bringing Swift into your Objective-C Projects

but…

Page 76: Bringing Swift into your Objective-C Projects

there’s a Swift version!

Page 77: Bringing Swift into your Objective-C Projects

let me just go update my podfile…

Page 78: Bringing Swift into your Objective-C Projects

Mines of Moria

Page 79: Bringing Swift into your Objective-C Projects

cannot package Swift in static libraries

Page 80: Bringing Swift into your Objective-C Projects

wait what? really!?

Page 81: Bringing Swift into your Objective-C Projects

doesn’t CocoaPods output static libraries?

Page 82: Bringing Swift into your Objective-C Projects

bingo.

Page 83: Bringing Swift into your Objective-C Projects

use_frameworks!{if targeting iOS 8 and above}

Page 84: Bringing Swift into your Objective-C Projects

Encountered Balrog

Page 85: Bringing Swift into your Objective-C Projects

Parse SDK pod didn’t like living in a framework

Page 86: Bringing Swift into your Objective-C Projects

… domino effect

Page 87: Bringing Swift into your Objective-C Projects

So watch out, you will hit a snag…

Page 88: Bringing Swift into your Objective-C Projects

good news is,

Page 89: Bringing Swift into your Objective-C Projects

Parse SDK now works with use_frameworks!

Page 90: Bringing Swift into your Objective-C Projects

Ok, back to building the view controller

Page 91: Bringing Swift into your Objective-C Projects

looks like I need some logging…

Page 92: Bringing Swift into your Objective-C Projects

no problem, CocoaLumberjack’s

already in my podfile :]

Page 93: Bringing Swift into your Objective-C Projects

Mordor's Black Gate

Page 94: Bringing Swift into your Objective-C Projects

remember, Swift does not

support macros

Page 95: Bringing Swift into your Objective-C Projects

it’s ok because…

Page 96: Bringing Swift into your Objective-C Projects

CocoaLumberjack version 2.0.0

doesn’t macro

Page 97: Bringing Swift into your Objective-C Projects

there’s a migration guide on GitHub

Page 98: Bringing Swift into your Objective-C Projects

and don’t forget to run all your schemes

Page 99: Bringing Swift into your Objective-C Projects

build and run…

Page 100: Bringing Swift into your Objective-C Projects

Shelob

Page 101: Bringing Swift into your Objective-C Projects

Tweaks

ALSO based on macros…

Page 102: Bringing Swift into your Objective-C Projects

Just need to replace macros with Swift code

Page 103: Bringing Swift into your Objective-C Projects

Check out ‘Using from a Swift Project’ section in

Tweaks README on GitHub

Page 104: Bringing Swift into your Objective-C Projects

Things we ran into

• Lumberjack

• Masonry/SnapKit

• Cocoapods / Parse

• Tweaks

Page 105: Bringing Swift into your Objective-C Projects

don’t let this scare you

Page 106: Bringing Swift into your Objective-C Projects

Swift is being adopted at an incredible pace

Page 107: Bringing Swift into your Objective-C Projects

Ok, view controller complete…

Page 108: Bringing Swift into your Objective-C Projects

time to plug it in

Page 109: Bringing Swift into your Objective-C Projects

in code or

Interface Builder

Page 110: Bringing Swift into your Objective-C Projects

In code

in container view controller #import {module}-Swift.h

Page 111: Bringing Swift into your Objective-C Projects

Interface Builder

do nothing! (special)

Page 112: Bringing Swift into your Objective-C Projects

{1} Jump

{2} One View Controller

{3} Everything above view controller

{1} Jump

{2} One View Controller

{3} Everything above view controller

Learn

Page 113: Bringing Swift into your Objective-C Projects

{3} Everything Above

View Controller

Page 114: Bringing Swift into your Objective-C Projects

moved on up to root view controller

Page 115: Bringing Swift into your Objective-C Projects

Core Logic

User Interface

UIApplication

UIApplicationDelegate UIWindow

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewControllerUIViewController

UIViewController

UIView UIView

UIView

Page 116: Bringing Swift into your Objective-C Projects

Core Logic

User Interface

UIApplication

UIApplicationDelegate UIWindow

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewControllerUIViewController

UIViewController

UIView UIView

UIView

Page 117: Bringing Swift into your Objective-C Projects

and up again…

Page 118: Bringing Swift into your Objective-C Projects

Core Logic

User Interface

UIApplication

UIApplicationDelegate UIWindow

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewControllerUIViewController

UIViewController

UIView UIView

UIView

Page 119: Bringing Swift into your Objective-C Projects

Core Logic

User Interface

UIApplication

UIApplicationDelegate UIWindow

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewControllerUIViewController

UIViewController

UIView UIView

UIView

Page 120: Bringing Swift into your Objective-C Projects

So far

Page 121: Bringing Swift into your Objective-C Projects

So far

• We implemented a new view controller in Swift

• We re-implemented root view controller in Swift

• We re-implemented app delegate in Swift

Page 122: Bringing Swift into your Objective-C Projects

We got a taste of Swift

Page 123: Bringing Swift into your Objective-C Projects

{1} Jump

{2} One View Controller

{3} Everything above view controller

{1} Jump

{2} One View Controller

{3} Everything above view controller

Learn

Page 124: Bringing Swift into your Objective-C Projects

this next step is crucial

Page 125: Bringing Swift into your Objective-C Projects

Maximize BenefitsBig Idea 2

Page 126: Bringing Swift into your Objective-C Projects

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

Maximize Benefits

Page 127: Bringing Swift into your Objective-C Projects

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

Maximize Benefits

Page 128: Bringing Swift into your Objective-C Projects

{1} Think Vertical

Probably the most important topic of this talk.

Page 129: Bringing Swift into your Objective-C Projects

it’s tempting to continue building view controllers

Page 130: Bringing Swift into your Objective-C Projects

it’s also tempting to move on to core logic

Page 131: Bringing Swift into your Objective-C Projects

but first,

Page 132: Bringing Swift into your Objective-C Projects

let’s understand how Swift can help

make your apps better

Page 133: Bringing Swift into your Objective-C Projects

better means material improvement

Page 134: Bringing Swift into your Objective-C Projects

actually improving your app

Page 135: Bringing Swift into your Objective-C Projects

if Swift can’t have a material impact on

your product…

Page 136: Bringing Swift into your Objective-C Projects

then WHO CARES!

Page 137: Bringing Swift into your Objective-C Projects

but Swift can!

Page 138: Bringing Swift into your Objective-C Projects

How?

Page 139: Bringing Swift into your Objective-C Projects

Safety & Speed

Page 140: Bringing Swift into your Objective-C Projects

Let’s focus on safety

Page 141: Bringing Swift into your Objective-C Projects

Safety means less chance of crashing

Page 142: Bringing Swift into your Objective-C Projects

How

Page 143: Bringing Swift into your Objective-C Projects

How

• Static typing

• Enforcing nullability & non nullability

• Immutability & value semantics

Page 144: Bringing Swift into your Objective-C Projects

These are Swift only features

Page 145: Bringing Swift into your Objective-C Projects

Objective-C code that calls Swift code does not benefit

Page 146: Bringing Swift into your Objective-C Projects

So we want to use Swift types across as many

sub-systems as we can

Page 147: Bringing Swift into your Objective-C Projects

this means across UI - Core Logic boundary

Page 148: Bringing Swift into your Objective-C Projects

so how did we use this to our advantage?

Page 149: Bringing Swift into your Objective-C Projects

we went vertical

Page 150: Bringing Swift into your Objective-C Projects

we identified vertical slices

Page 151: Bringing Swift into your Objective-C Projects

what’s a vertical slice?

Page 152: Bringing Swift into your Objective-C Projects

Core Logic

User Interface

UIApplication

UIApplicationDelegate UIWindow

Persistence Networking Other Utilities

UIView

UIViewControllerUIViewController

UIViewController

UIView

UIView

Page 153: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

VC VC

V V

Vertical Slice

Page 154: Bringing Swift into your Objective-C Projects

we then re-factored our view controller to communicate with

core logic using Swift types

Page 155: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

VC

V

VC

V

P

N

O

Page 156: Bringing Swift into your Objective-C Projects

pure Swift core logic, only for pure Swift

view controllers

Page 157: Bringing Swift into your Objective-C Projects

Key Insight use Swift types across

the entire stack

Page 158: Bringing Swift into your Objective-C Projects

remember, we are trying to maximize

our use of Swift

Page 159: Bringing Swift into your Objective-C Projects

ok so that’s what we did, you might be

wondering…

Page 160: Bringing Swift into your Objective-C Projects

why not start at the bottom?

Page 161: Bringing Swift into your Objective-C Projects

why not just build the core logic in Swift?

Page 162: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

VC VC

V V

VC

V

VC

V

VC

V

Page 163: Bringing Swift into your Objective-C Projects

I hear this a lot

Page 164: Bringing Swift into your Objective-C Projects

so quick sidebar, here’s the argument

Page 165: Bringing Swift into your Objective-C Projects

{this goes for codebases that will be worked on for

the foreseeable future}

Page 166: Bringing Swift into your Objective-C Projects

let’s say we decide to do this

Page 167: Bringing Swift into your Objective-C Projects

if we go horizontal…

Page 168: Bringing Swift into your Objective-C Projects

we won’t maximize our use of Swift

Page 169: Bringing Swift into your Objective-C Projects

and we’ll paint ourselves into a corner

Page 170: Bringing Swift into your Objective-C Projects

why?

Page 171: Bringing Swift into your Objective-C Projects

if we start at the bottom, we’ll definitely have Objective-C

consuming our Swift code

Page 172: Bringing Swift into your Objective-C Projects

Why is that bad?

Page 173: Bringing Swift into your Objective-C Projects

we’ll end up designing for least common

denominator

Page 174: Bringing Swift into your Objective-C Projects

Objective-C View Controller

Objective-C View Controller

Objective-C View Controller Objective-C View Controller

Core Logic Objective-CSuperclass

Swift Core Logic

Core Logic Objective-CSuperclass

Swift Core Logic

Core Logic Objective-CSuperclass

Swift Core Logic

Page 175: Bringing Swift into your Objective-C Projects

we’ll want Objective-C and Swift view controllers

to use our Swift base

Page 176: Bringing Swift into your Objective-C Projects

Objective-C View Controller

Swift View Controller

Objective-C View Controller

Objective-C View Controller Objective-C View Controller

Core Logic Objective-CSuperclass

Swift Core Logic

Core Logic Objective-CSuperclass

Swift Core Logic

Core Logic Objective-CSuperclass

Swift Core Logic

Page 177: Bringing Swift into your Objective-C Projects

we’ll end up building non Swifty interfaces

between UI and core logic

Page 178: Bringing Swift into your Objective-C Projects

this will leak deep into both UI and core logic

Page 179: Bringing Swift into your Objective-C Projects

Objective-C Superclass

Swift Subclass

Objective-C ObjectObjective-C Object

Objective-C Object

Objective-C Object

Swift Class Object

Swift Class Object

Page 180: Bringing Swift into your Objective-C Projects

Objective-C Superclass

Swift Subclass

Objective-C ObjectObjective-C Object

Objective-C Object

Objective-C Object

Swift Class Object

Swift Class Object

Page 181: Bringing Swift into your Objective-C Projects

& will continue over time

Page 182: Bringing Swift into your Objective-C Projects

let’s say we decide to go pure Swift and we re-write all of our view controllers

Page 183: Bringing Swift into your Objective-C Projects

without a re-factor, those Swift view controllers will interface with existing core logic layer

Page 184: Bringing Swift into your Objective-C Projects

and we’ll end up with a pure Swift codebase that doesn’t

use the safety features

Page 185: Bringing Swift into your Objective-C Projects

that’s crazy

Page 186: Bringing Swift into your Objective-C Projects

but you say, there’s a way around this…

Page 187: Bringing Swift into your Objective-C Projects

Objective-C Core Logic Superclass

Swift Core Logic Subclass

Objective-C View Controller Objective-C View Controller Swift View Controller

Page 188: Bringing Swift into your Objective-C Projects

why go through all these

gymastics?

Page 189: Bringing Swift into your Objective-C Projects

• Everyone on the team will have to understand this

• Requires a ton of discipline & deep understanding

Page 190: Bringing Swift into your Objective-C Projects

another way to look at it is…

Page 191: Bringing Swift into your Objective-C Projects

we’ll end up using only a portion of Swift,

Page 192: Bringing Swift into your Objective-C Projects

Accessible • Classes• Reference Semantics• Int backed Enums

Unaccessible • Generics• Tuples• Enumerations defined in Swift without Int raw

value type• Structures defined in Swift• Top-level functions defined in Swift• Global variables defined in Swift• Typealiases defined in Swift• Swift-style variadics• Nested types• Curried functions

Page 193: Bringing Swift into your Objective-C Projects

Yes, we can start at the bottom

Page 194: Bringing Swift into your Objective-C Projects

and yes, it will work

Page 195: Bringing Swift into your Objective-C Projects

but we’re not taking advantage of everything

Swift has to offer

Page 196: Bringing Swift into your Objective-C Projects

Ok, sidebar over…

Page 197: Bringing Swift into your Objective-C Projects

instead

Page 198: Bringing Swift into your Objective-C Projects

find new or existing vertical slices you can

write in Swift

Page 199: Bringing Swift into your Objective-C Projects

if it’s not clear where the vertical slices are…

Page 200: Bringing Swift into your Objective-C Projects

re-factor your Objective-C code first

Page 201: Bringing Swift into your Objective-C Projects

that’s safety

Page 202: Bringing Swift into your Objective-C Projects

Let’s talk Speed

Page 203: Bringing Swift into your Objective-C Projects

boils down to essentially no

runtime overhead

Page 204: Bringing Swift into your Objective-C Projects

that’s speed

Page 205: Bringing Swift into your Objective-C Projects

Knowing Swift’s strengths, you can improve your

existing apps with Swift code

Page 206: Bringing Swift into your Objective-C Projects

and gradually expand its surface area within

your project

Page 207: Bringing Swift into your Objective-C Projects

back to the journey…

Page 208: Bringing Swift into your Objective-C Projects

Ready to test!

Page 209: Bringing Swift into your Objective-C Projects

not so fast…

Page 210: Bringing Swift into your Objective-C Projects

Enterprise codesign

Page 211: Bringing Swift into your Objective-C Projects

Ready to ship!

Page 212: Bringing Swift into your Objective-C Projects

not so fast…

Page 213: Bringing Swift into your Objective-C Projects

Conditional Compile

Page 214: Bringing Swift into your Objective-C Projects
Page 215: Bringing Swift into your Objective-C Projects
Page 216: Bringing Swift into your Objective-C Projects
Page 217: Bringing Swift into your Objective-C Projects

#if Debug …

#elseif Release …

#endif

Page 218: Bringing Swift into your Objective-C Projects

Google #ifdef replacement in swift

Page 219: Bringing Swift into your Objective-C Projects

Time to cut release build and…

Page 220: Bringing Swift into your Objective-C Projects

ship it!

Page 221: Bringing Swift into your Objective-C Projects
Page 222: Bringing Swift into your Objective-C Projects

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

Maximize Benefits

Page 223: Bringing Swift into your Objective-C Projects

{2} Swift will Change

Page 224: Bringing Swift into your Objective-C Projects
Page 225: Bringing Swift into your Objective-C Projects

Then Apple released Swift 1.2

Page 226: Bringing Swift into your Objective-C Projects

it’s just a dot release

Page 227: Bringing Swift into your Objective-C Projects

piece of cake, there’s even a migrator :]

Page 228: Bringing Swift into your Objective-C Projects

‘cause it’s just a dot release

Page 229: Bringing Swift into your Objective-C Projects

right?

Page 230: Bringing Swift into your Objective-C Projects

nope!

Page 231: Bringing Swift into your Objective-C Projects

took two full days to migrate

Page 232: Bringing Swift into your Objective-C Projects

Swift is still evolving

Page 233: Bringing Swift into your Objective-C Projects

good news is…

Page 234: Bringing Swift into your Objective-C Projects

you don’t have to uptake new versions

immediately {for now}

Page 235: Bringing Swift into your Objective-C Projects

point is, you WILL run into this

Page 236: Bringing Swift into your Objective-C Projects

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

Maximize Benefits

Page 237: Bringing Swift into your Objective-C Projects

{3} Plan for the Future

Page 238: Bringing Swift into your Objective-C Projects

things are good

Page 239: Bringing Swift into your Objective-C Projects

time to think about the long haul

Page 240: Bringing Swift into your Objective-C Projects

Game plans

Page 241: Bringing Swift into your Objective-C Projects

Game plans

• Mixed Project

• Modularize

• Re-write (Go big or go home)

Page 242: Bringing Swift into your Objective-C Projects

Mixed Project Game plan

Page 243: Bringing Swift into your Objective-C Projects

Modularize Game plan

Page 244: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

VC VC

V V

VC

V

VC

V

VC

V

Persistence

Networking

Other Utilities

VC

V

VC

V

Page 245: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

Persistence

Networking

Other Utilities

VC

V

VC

V

Module / Cocoa Touch Framework

VC VC

V V

VC

V

VC

V

VC

V

Page 246: Bringing Swift into your Objective-C Projects

You Might Run Into

Page 247: Bringing Swift into your Objective-C Projects

You Might Run Into

• Exporting Module Maps

• Some system frameworks aren’t mapped to modules yet (common crypto, libxml, etc.)

Page 248: Bringing Swift into your Objective-C Projects

Google, “clang module map”

Page 249: Bringing Swift into your Objective-C Projects

Re-write Game plan

Page 250: Bringing Swift into your Objective-C Projects

You don’t have to re-write your app

Page 251: Bringing Swift into your Objective-C Projects

as a matter of fact you probably shouldn’t

Page 252: Bringing Swift into your Objective-C Projects

best game plan depends on your app’s architectural topology

Page 253: Bringing Swift into your Objective-C Projects

depends a lot on the size of the code base and how well architected/structured it is

Page 254: Bringing Swift into your Objective-C Projects

can you modularize it?

and how can you slice it?

Page 255: Bringing Swift into your Objective-C Projects

3 Gameplans

• Go big or go home

• Modularize

• Mixed

Page 256: Bringing Swift into your Objective-C Projects

we decided…

Page 257: Bringing Swift into your Objective-C Projects

Go big or go home

Page 258: Bringing Swift into your Objective-C Projects

Mordor

Page 259: Bringing Swift into your Objective-C Projects

this is the least practical path…

Page 260: Bringing Swift into your Objective-C Projects

but why not?

Page 261: Bringing Swift into your Objective-C Projects

we re-wrote all of our vertical slices

one by one

Page 262: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

Persistence

Networking

Other Utilities

UIViewController UIView

VC

V

VC

V

P

N

O

P

N

O

VC

V

VC

V

P

N

O

VC

V

P

N

O

VC

V

P

N

O

Page 263: Bringing Swift into your Objective-C Projects

UIApplication

UIApplicationDelegate

UIViewController UIView

VC

V

VC

V

VC

V

VC

V

VC

V

Persistence

Networking

Other Utilities

Page 264: Bringing Swift into your Objective-C Projects

we made it!

Page 265: Bringing Swift into your Objective-C Projects

pure Swift goodness

Page 266: Bringing Swift into your Objective-C Projects

and we shipped it!!!

Page 267: Bringing Swift into your Objective-C Projects
Page 268: Bringing Swift into your Objective-C Projects
Page 269: Bringing Swift into your Objective-C Projects

Steps we took

• Identified Vertical Slices

• Re-wrote each vertical slice one at a time

• Maximized our use of Swift

Page 270: Bringing Swift into your Objective-C Projects

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

{1} Think Vertical

{2} Swift will Change

{3} Plan for the Future

Maximize Benefits

Page 271: Bringing Swift into your Objective-C Projects

Two Big Ideas

1. Learn by Doing, but not doing too much

2. Maximize Benefits

Page 272: Bringing Swift into your Objective-C Projects

that was quite a journey

Page 273: Bringing Swift into your Objective-C Projects

what happened next?

Page 274: Bringing Swift into your Objective-C Projects

new project

Page 275: Bringing Swift into your Objective-C Projects

pure Swift project

Page 276: Bringing Swift into your Objective-C Projects

having done mixed to full helped A TON

Page 277: Bringing Swift into your Objective-C Projects

we shipped that too…

Page 278: Bringing Swift into your Objective-C Projects

just a week ago

Page 279: Bringing Swift into your Objective-C Projects

and now we are here

Page 280: Bringing Swift into your Objective-C Projects

Whew…

Page 281: Bringing Swift into your Objective-C Projects

The Point

Page 282: Bringing Swift into your Objective-C Projects

learned a lot along the way without risking too

much at any time

Page 283: Bringing Swift into your Objective-C Projects

you can start writing Swift right now

without worrying

Page 284: Bringing Swift into your Objective-C Projects

you have options

Page 285: Bringing Swift into your Objective-C Projects

What Advice Can I Give?So what did I learn?

Page 286: Bringing Swift into your Objective-C Projects

What Advice Can I Give?

• What to learn first

• Objective-C consuming Swift interop

• Objective-C selectors in Swift

Page 287: Bringing Swift into your Objective-C Projects

What to learn first

Page 288: Bringing Swift into your Objective-C Projects

What to learn first

• Optionals, optionals, and optionals!

• Reference types vs Value types

• Initializers, BE CAREFUL

• Classes (inheritance, overrides, etc.)

• New Objective-C features (nullability, etc)

Page 289: Bringing Swift into your Objective-C Projects

Exposing Swift to Objective-C

Page 290: Bringing Swift into your Objective-C Projects

Objective-C can’t see pure Swift

Page 291: Bringing Swift into your Objective-C Projects
Page 292: Bringing Swift into your Objective-C Projects

Tips

Page 293: Bringing Swift into your Objective-C Projects

Avoid writing pure Swift code for

Objective-C components

Page 294: Bringing Swift into your Objective-C Projects

Stick to subclassing Apple framework classes

Page 295: Bringing Swift into your Objective-C Projects

or create your own Objective-C super

classes

Page 296: Bringing Swift into your Objective-C Projects

Objective C Superclass

Pure Swift Class, Struct, Enum

Swift subclass

Pure Swift Class, Struct, EnumPure Swift Class, Struct, Enum

Pure Swift Class, Struct, Enum

Objective C Consumer

Swift talks to Swift, Objective-C talks to

Objective-C

Page 297: Bringing Swift into your Objective-C Projects
Page 298: Bringing Swift into your Objective-C Projects

Yes, you CAN get Objective-C talking to

Swift classes, but why?

Page 299: Bringing Swift into your Objective-C Projects

Don’t have to wonder

• Can Objective-C take value types?

• Can Objective-C take my Swift enum?

• Can Objective-C take my closure?

• Do I need an adapter?

• etc.

Page 300: Bringing Swift into your Objective-C Projects

“If you never import a Swift class in Objective-C code, you

don’t need to worry about type compatibility” - Apple

Page 301: Bringing Swift into your Objective-C Projects

let’s talk Objective-C Selectors

Page 302: Bringing Swift into your Objective-C Projects

Swift is static, not dynamic

Page 303: Bringing Swift into your Objective-C Projects

we have to understand how Objective-C selectors work

in a static Swift world

Page 304: Bringing Swift into your Objective-C Projects

when to use @objc and @dynamic

Page 305: Bringing Swift into your Objective-C Projects

some quotes from our friends at Apple

Page 306: Bringing Swift into your Objective-C Projects

“Requiring @dynamic dispatch is rarely

necessary”

Page 307: Bringing Swift into your Objective-C Projects

“you must use the @dyanamic modifier when you know that

the implementation of an API is replaced at runtime"

Page 308: Bringing Swift into your Objective-C Projects

“If your Swift class inherits from an Objective-C class, all of the methods

and properties in the class are available as Objective-C selectors.”

Page 309: Bringing Swift into your Objective-C Projects

This is important when

• using target-action

• using Key Value Coding / Key Value Observing

• (oh no!) swizzling

Page 310: Bringing Swift into your Objective-C Projects

Wrapping up…

Page 311: Bringing Swift into your Objective-C Projects

I survived

Page 312: Bringing Swift into your Objective-C Projects

you can do it too

Page 313: Bringing Swift into your Objective-C Projects

you don’t have to jump in all at once

Page 314: Bringing Swift into your Objective-C Projects

do it your way, at your pace

Page 315: Bringing Swift into your Objective-C Projects

learn the language basics & interop

Page 316: Bringing Swift into your Objective-C Projects

so that then you can look at the benefits of Swift to improve your apps

Page 317: Bringing Swift into your Objective-C Projects

plan it out, be intentional

Page 318: Bringing Swift into your Objective-C Projects

you don’t have to re-write anything

Page 319: Bringing Swift into your Objective-C Projects

you don’t have to compromise your

team’s commitments

Page 320: Bringing Swift into your Objective-C Projects

there will be bumps

Page 321: Bringing Swift into your Objective-C Projects

you know what to expect

Page 322: Bringing Swift into your Objective-C Projects

you are ready

Page 323: Bringing Swift into your Objective-C Projects

New File… that’s all it takes

Page 324: Bringing Swift into your Objective-C Projects

Go Swift

Page 325: Bringing Swift into your Objective-C Projects

The end. René CacheauxClient [email protected]

@[email protected]