OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift...

39
OBJECTIVE-C, MEET SWIFT HOW TO INTRODUCE SWIFT INTO AN OBJECTIVE-C CODEBASE

Transcript of OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift...

Page 1: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

OBJECTIVE-C, MEET SWIFTHOW TO INTRODUCE SWIFT INTO AN OBJECTIVE-C CODEBASE

Page 2: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

INTRODUCTION

Page 3: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Jake Carter

▸ Software Engineer, Omni Group — 2011 - Present

▸ Instructor, UW — 2014 - 2016

▸ Software Engineer, RogueSheep — 2008 - 2011

Page 4: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Agenda

▸ Why Swift?

▸ My Favorite Features

▸ HOWTO: Swift

▸ Adding Swift to an Objective-C App & Framework

Page 5: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

WHY SWIFT?MY FAVORITE FEATURES

Page 6: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Overview

▸ High Performance & High Productivity

▸ Modern: Multiple Return Values (Tuples), Optional Arguments, Closures, Generics, Type Inference

▸ Safe: No uninitialized data, Promotes immutability, Array bounds checks, Integer overflow checks, Raw pointers marked "unsafe"

▸ Fast: ARM & x86-64 native, Tuned native collections, Swift-specific optimizer, C-like procedural performance

Page 7: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

My Favorite Features

▸ Mutability

▸ Value Type vs Reference Type

▸ let vs var

▸ Optionals

▸ To nil or not to nil, that is the question

▸ Generics

▸ Strongly typed collections

Page 8: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

MUTABILITY

Page 9: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Mutability

▸ Changing the object in a variable

▸ Changing the state of an object

Page 10: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Value Type vs Reference Type

▸ Value Types

▸ Struct, Enum

▸ Reference Types

▸ Class, Closure, @objc, id

Page 11: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Constants vs Variables

let constant: Type = value

var variable: Type = initial value

Page 12: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let vs var: Value Type

struct Card { var rank: String var suit: String }

Page 13: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let queen = Card(rank: "queen", suit: "hearts") var anotherQueen = queen anotherQueen.suit = "diamonds"

Card(rank: "queen", suit: "hearts")

let vs var: Value Type

let =Card

“queen”, “hearts”queen

Page 14: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let queen = Card(rank: "queen", suit: "hearts") var anotherQueen = queen anotherQueen.suit = "diamonds"

Card “queen”, “hearts”

Card “queen”, “hearts”

Card(rank: "queen", suit: "hearts")

let vs var: Value Type

let =

var =

Card “queen”, “hearts”

queen

queenanotherQueen

Page 15: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let queen = Card(rank: "queen", suit: "hearts") var anotherQueen = queen anotherQueen.suit = "diamonds"

Card “queen”, “hearts”

Card “queen”, “diamonds”

Card(rank: "queen", suit: "hearts")

let vs var: Value Type

let =

var =

Card “queen”, “hearts”

queen

queenanotherQueen

Page 16: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let vs var: Reference Type

class Person { var name: String var birthDate: String }

Page 17: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let frances = Person(name: "Frances", birthDate: "01/01/1983") var anotherFrances = frances anotherFrances.birthDate = "01/02/1983"

Person "Frances", "…"

Reference Types

Person(name: "Frances", birthDate: "…")Person

"Frances", "…"franceslet =

anotherFrancesvar = frances

Page 18: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

let vs var

let var

Value Type Cannot change object; Cannot mutate state

Can change object; Can mutate state*

Reference Type

Cannot change object; Can mutate state

Can change object; Can mutate state

*Functions that self-change Value Type must be marked mutating.

Page 19: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Value Type vs Reference Type

struct Card { var rank: String var suit: String }

class Person { var name: String var birthDate: String }

Page 20: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

OPTIONALS

Page 21: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Non-Optional Type

var name: String = “Margaret” name = nil

NIL CANNOT BE ASSIGNED TO TYPE 'STRING'

Page 22: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Optional Type

var name: String? = “Margaret” name = nil

Page 23: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Optional Type

var name: String? = “Margaret” name = nil

let chars = name.charactersVALUE OF OPTIONAL TYPE 'STRING?' NOT UNWRAPPED;

Page 24: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Optional Binding

var name: String? = “Margaret” name = nil

if let name = name { let chars = name.characters }

Page 25: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Optional Chaining

var name: String? = “Margaret” name = nil

if let name = name { let chars = name.characters }

let chars = name?.characters

Page 26: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Optional Binding vs Optional Chaining

var name: String? = “Margaret” name = nil

if let name = name { let chars = name.characters }

let chars = name?.characterslet chars: CharacterView

let chars: CharacterView?

Page 27: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

GENERICS

Page 28: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Generic Collections

struct Array<Element> { … }

struct Dictionary<Key: Hashable, Value> { … }

Page 29: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Array

let names: Array<String> = ["Foo", … ]

Page 30: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Array

let names: Array<String> = ["Foo", … ]

let names: [String] = ["Foo", … ]

Page 31: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Array

let names: Array<String> = ["Foo", … ]

let names: [String] = ["Foo", … ]

let names = ["Foo", … ]

Page 32: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Dictionary

let nameAge: Dictionary<String, Int> = ["Pam" : 36, … ]

let nameAge: [String : Int] = ["Pam" : 36, … ]

let nameAge = ["Pam" : 36, … ]

Page 33: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

HOWTO: SWIFTADDING SWIFT TO AN OBJECTIVE-C APP & FRAMEWORK

Page 34: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

DEMO

Page 35: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Demo Wrap Up

▸ Added Swift to Objective-C App Target

▸ Bridging Header, Enabled Module Support, Subclassed Objective-C class in Swift

▸ Swiftified Objective-C Framework Headers

▸ Nullability Annotations, Typed Collections

▸ Added Swift to Objective-C Framework Target

▸ NO Bridging Header/Must use Umbrella Header, Enabled Module Support, Extended Objective-C class in Swift

▸ Utilized Framework Swift in App

Page 36: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Bridging Headers (From same App Target)

Import into Swift Import into Objective-C

Swift No import statement#import

“ProductModuleName-Swift.h”

Objective-CNo import statement; Objective-C bridging

header required#import “Header.h”

Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 3).” iBooks. https://itun.es/us/1u3-0.l

Page 37: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Bridging Headers (From same Framework Target)

Import into Swift Import into Objective-C

Swift No import statement#import <ProductName/

ProductModuleName-Swift.h>

Objective-CNo import statement; Objective-C umbrella

header required#import “Header.h”

Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 3).” iBooks. https://itun.es/us/1u3-0.l

Page 38: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

Importing Frameworks

Import into Swift Import into Objective-C

Any language framework import FrameworkName @import FrameworkName;

Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 3).” iBooks. https://itun.es/us/1u3-0.l

Page 39: OBJECTIVE-C, MEET SWIFT - averagejake.com fileAgenda Why Swift? My Favorite Features HOWTO: Swift Adding Swift to an Objective-C App & Framework

THANK YOU

@JakeCarter AverageJake.com