Slide 1

Slide 1 text

Swift An introduction to the language

Slide 2

Slide 2 text

Kris Arnold Shutterstock @wka @shuttertech

Slide 3

Slide 3 text

Disclaimer The only people with more than a few months experience with Swift work at Apple

Slide 4

Slide 4 text

Topics Language Design Principles Why a new language? Variables and Data Structures Flow Control Functions and Closures Classes and Protocols Structs and Enums Generics What Else? What's Missing? What's Next?

Slide 5

Slide 5 text

Language Design Principles

Slide 6

Slide 6 text

Language Design Principles Speed Safety Modern features Interoperability with Objective-C

Slide 7

Slide 7 text

Why?

Slide 8

Slide 8 text

Cruft

Slide 9

Slide 9 text

No namespaces NSObject SSIImage

Slide 10

Slide 10 text

nil NULL NSNull [NSNull null] NSNotFound Nil

Slide 11

Slide 11 text

Variables and Data Structures

Slide 12

Slide 12 text

var place = "World"

Slide 13

Slide 13 text

var place = "World" println("Hello, \(place)")

Slide 14

Slide 14 text

let place = "World" println("Hello, \(place)")

Slide 15

Slide 15 text

let place = "World" println("Hello, \(place)") place = "North America" //error!

Slide 16

Slide 16 text

var place = "World" println("Hello, \(place)") place = "North America"

Slide 17

Slide 17 text

var place = "World"

Slide 18

Slide 18 text

var place: String = "World"

Slide 19

Slide 19 text

var cities = [String]()

Slide 20

Slide 20 text

var cities = [String]() cities = ["New York", "Berlin"]

Slide 21

Slide 21 text

var cities = [String]() var cities = ["New York", "Berlin"]

Slide 22

Slide 22 text

var cities = ["New York", "Berlin"]

Slide 23

Slide 23 text

var cities = ["New York", "Berlin"] cities[0] // "New York"

Slide 24

Slide 24 text

var cities = ["New York", "Berlin"] cities[0] // "New York" cities.append("San Francisco")

Slide 25

Slide 25 text

var cities = ["New York", "Berlin"] cities[0] // "New York" cities.append("San Francisco") cities.insert("Denver", atIndex: 0)

Slide 26

Slide 26 text

var cities = ["New York", "Berlin"] cities[0] // "New York" cities.append("San Francisco") cities.insert("Denver", atIndex: 0) cities.count // 4

Slide 27

Slide 27 text

var populations = [String: Int]()

Slide 28

Slide 28 text

var populations = [String: Int]() populations = ["New York": 8_300_000, "Berlin": 3_500_000]

Slide 29

Slide 29 text

var populations = [String: Int]() populations = ["New York": 8_300_000, "Berlin": 3_500_000] populations["Denver"] = 600_000 populations["San Francisco"] = 800_000

Slide 30

Slide 30 text

var populations = [String: Int]() populations = ["New York": 8_300_000, "Berlin": 3_500_000] populations["Denver"] = 600_000 populations["San Francisco"] = 800_000 populations["New York"] // 8_300_000

Slide 31

Slide 31 text

("nyc", 8_300_000)

Slide 32

Slide 32 text

var nameAndPop = ("nyc", 8_300_000)

Slide 33

Slide 33 text

var nameAndPop = ("nyc", 8_300_000) nameAndPop.0 // "nyc"

Slide 34

Slide 34 text

var nameAndPop = (city: "nyc", population: 8_300_000)

Slide 35

Slide 35 text

var nameAndPop = (city: "nyc", population: 8_300_000) nameAndPop.city // "nyc"

Slide 36

Slide 36 text

Optionals

Slide 37

Slide 37 text

var name: String

Slide 38

Slide 38 text

var name: String var nickname: String?

Slide 39

Slide 39 text

var name: String var nickname: String? name = nil // error

Slide 40

Slide 40 text

var name: String var nickname: String? name = nil // error nickname = nil // OK

Slide 41

Slide 41 text

populations["New York"]

Slide 42

Slide 42 text

populations["New York"] // return type: Int?

Slide 43

Slide 43 text

populations["New York"] // return type: Int? populations["Atlantis"] // return type: Int? // return value: nil

Slide 44

Slide 44 text

Flow Control

Slide 45

Slide 45 text

if … then … else for … in loops C-style for loops While loops Do-while loops

Slide 46

Slide 46 text

var nickname: String?

Slide 47

Slide 47 text

var nickname: String? … if let moniker = nickname { println("Hello \(moniker)") }

Slide 48

Slide 48 text

var nickname: String? … if let moniker = nickname { println("Hello \(moniker)") } else { println("Hello friend") }

Slide 49

Slide 49 text

var nickname: String? … println("Hello \(nickname!)")

Slide 50

Slide 50 text

var nickname: String? …

Slide 51

Slide 51 text

var nickname: String? … nickname?.extend(" :)")

Slide 52

Slide 52 text

switch statements Demo

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Functions and Closures

Slide 55

Slide 55 text

func sayHello() -> () { println "Hello" }

Slide 56

Slide 56 text

func sayHello(name: String) -> () { println "Hello \(name)" }

Slide 57

Slide 57 text

func sayHello(name: String) -> () { println "Hello \(name)" } sayHello("Lattner") // prints "Hello Lattner"

Slide 58

Slide 58 text

func sayHello(name: String) -> () { println "Hello \(name)" } sayHello(name: "Lattner") // error!

Slide 59

Slide 59 text

func sayHello(person name: String) -> () { println "Hello \(name)" } sayHello(person: "Lattner") // correct

Slide 60

Slide 60 text

func sayHello(#name: String) -> () { println "Hello \(name)" } sayHello(name: "Lattner") // correct

Slide 61

Slide 61 text

func sayHello(name: String = "You") -> () { println "Hello \(name)" }

Slide 62

Slide 62 text

func sayHello(name: String) -> () { println "Hello \(name)" }

Slide 63

Slide 63 text

func sayHello(firstName: String, lastName: String) -> () { println "Hello \(firstName) \(lastName)" }

Slide 64

Slide 64 text

func sayHello(name: String) -> () { println "Hello \(name)" }

Slide 65

Slide 65 text

func sayHello(name: String) -> (String) { return "Hello \(name)" }

Slide 66

Slide 66 text

func sayHello(name: String) -> (String) { name += "!" return "Hello \(name)" }

Slide 67

Slide 67 text

func sayHello(name: String) -> (String) { name += "!" // error! return "Hello \(name)" }

Slide 68

Slide 68 text

func sayHello(var name: String) -> (String) { name += "!" return "Hello \(name)" }

Slide 69

Slide 69 text

func sayHello(var name: String) -> (String) { name += "!" return "Hello \(name)" } var person = "Lattner" sayHello(person)

Slide 70

Slide 70 text

func sayHello( name: String) -> (String) { name += "!" return "Hello \(name)" } var person = "Lattner" sayHello( person) println("Updated to \(person)") // "Updated to Lattner!"

Slide 71

Slide 71 text

func sayHello(inout name: String) -> (String) { name += "!" return "Hello \(name)" } var person = "Lattner" sayHello(&person) println("Updated to \(person)") // "Updated to Lattner!"

Slide 72

Slide 72 text

Closures

Slide 73

Slide 73 text

var items = [100, 2, 75, 31, 99] items.sort // items is now [2, 31, 75, 99, 100]

Slide 74

Slide 74 text

var items = [100, 2, 75, 31, 99] let sorter = { } items.sort(sorter)

Slide 75

Slide 75 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) } items.sort(sorter)

Slide 76

Slide 76 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) -> Bool in } items.sort(sorter)

Slide 77

Slide 77 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) -> Bool in } items.sort(sorter)

Slide 78

Slide 78 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) -> Bool in return num1 < num2 } items.sort(sorter)

Slide 79

Slide 79 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) -> Bool in return num1 < num2 } items.sort(sorter)

Slide 80

Slide 80 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) -> Bool in num1 < num2 } items.sort(sorter)

Slide 81

Slide 81 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1: Int, num2: Int) in num1 < num2 } items.sort(sorter)

Slide 82

Slide 82 text

var items = [100, 2, 75, 31, 99] let sorter = { (num1, num2) in num1 < num2 } items.sort(sorter)

Slide 83

Slide 83 text

var items = [100, 2, 75, 31, 99] items.sort({(num1, num2) in num1 < num2})

Slide 84

Slide 84 text

var items = [100, 2, 75, 31, 99] items.sort({ num1, num2 in num1 < num2})

Slide 85

Slide 85 text

var items = [100, 2, 75, 31, 99] items.sort({ $0 < $1 })

Slide 86

Slide 86 text

var items = [100, 2, 75, 31, 99] items.sort({ $0 < $1 })

Slide 87

Slide 87 text

var items = [100, 2, 75, 31, 99] items.sort { $0 < $1 }

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let evens = list.filter { $0 % 2 == 0 }

Slide 90

Slide 90 text

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let evens = list.filter { $0 % 2 == 0 } let tenX = list.map { $0 * 10 }

Slide 91

Slide 91 text

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let evens = list.filter { $0 % 2 == 0 } let tenX = list.map { $0 * 10 } let sum = list.reduce(0) { $0 + $1 }

Slide 92

Slide 92 text

func makeMultiplier(number: Int) -> Int -> (Int) { }

Slide 93

Slide 93 text

func makeMultiplier(number: Int) -> Int -> (Int) { }

Slide 94

Slide 94 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { } }

Slide 95

Slide 95 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { (given: Int) -> Int in return number * given } }

Slide 96

Slide 96 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { (given: Int) -> Int in return number * given } return multiplier }

Slide 97

Slide 97 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { (given: Int) -> Int in return number * given } return multiplier } let timesTwenty = makeMultiplier(20)

Slide 98

Slide 98 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { (given: Int) -> Int in return number * given } return multiplier } let timesTwenty = makeMultiplier(20) timesTwenty(3) // output == 60

Slide 99

Slide 99 text

func makeMultiplier(number: Int) -> Int -> (Int) { let multiplier = { (given: Int) -> Int in return number * given } return multiplier } let timesTwenty = makeMultiplier(20) timesTwenty(3) // output == 60 let timesFifty = makeMultiplier(50) timesFifty(3) // output == 150

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

let priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND let queue = dispatch_get_gloabl_queue(priority, 0)

Slide 102

Slide 102 text

let priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND let queue = dispatch_get_gloabl_queue(priority, 0) dispatch_async(queue) { longRunningOperation() longerRunningOperation() }

Slide 103

Slide 103 text

let priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND let queue = dispatch_get_gloabl_queue(priority, 0) dispatch_async(queue) { longRunningOperation() longerRunningOperation() }

Slide 104

Slide 104 text

Classes and Protocols

Slide 105

Slide 105 text

class Image { }

Slide 106

Slide 106 text

class Image { var mediaId = 0 var description = "My awesome image" }

Slide 107

Slide 107 text

class Image { var mediaId = 0 var description = "My awesome image" } var myImage = Image() myImage.mediaId = 200_000

Slide 108

Slide 108 text

class Image { var mediaId var description }

Slide 109

Slide 109 text

class Image { var mediaId :Int var description :String }

Slide 110

Slide 110 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 111

Slide 111 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId } }

Slide 112

Slide 112 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 113

Slide 113 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } func approveImage() -> () { println("Approving…") } }

Slide 114

Slide 114 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } func approveImage() -> () { println("Approving…") } }

Slide 115

Slide 115 text

var myImage = Image( mediaId: 200_000, description: "Testing" )

Slide 116

Slide 116 text

var myImage = Image( mediaId: 200_000, description: "Testing" ) println("the description for \(myImage.mediaId) is \(myImage.description)")

Slide 117

Slide 117 text

var myImage = image( mediaId: 200_000, description: "Testing" ) println("the description for \(myImage.mediaId) is \(myImage.description)") myImage.approveImage()

Slide 118

Slide 118 text

var myImage = image( mediaId: 200_000, description: "Testing" ) println("the description for \(myImage.mediaId) is \(myImage.description)") myImage.approveImage() myImage.description = betterDescription

Slide 119

Slide 119 text

class Image { var mediaId :Int var description :String //… }

Slide 120

Slide 120 text

class Image { var mediaId :Int var description :String { } //… }

Slide 121

Slide 121 text

class Image { var mediaId :Int var description :String { didSet { } } //… }

Slide 122

Slide 122 text

class Image { var mediaId :Int var description :String { didSet { println("Changed desc to \(description)") updateAuditTrail(mediaId, description) } } //… }

Slide 123

Slide 123 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 124

Slide 124 text

class Image { let mediaId :Int let description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 125

Slide 125 text

class Image { let mediaId :Int let description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } } //… image.description = betterDescription // error!

Slide 126

Slide 126 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 127

Slide 127 text

class Image { var mediaId :Int var description :String var height :Int var width :Int … }

Slide 128

Slide 128 text

class Image { var mediaId :Int var description :String var height :Int var width :Int var aspectRatio :Float … }

Slide 129

Slide 129 text

class Image { var mediaId :Int var description :String var height :Int var width :Int var aspectRatio :Float { return Float(height) / Float(width) } … }

Slide 130

Slide 130 text

class Image { var mediaId :Int var description :String var height :Int var width :Int var aspectRatio :Float { return Float(height) / Float(width) } … }

Slide 131

Slide 131 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 132

Slide 132 text

class Image: Media { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description } }

Slide 133

Slide 133 text

class Image: Media { var mediaId :Int var description :String init(mediaId: Int, description: String) { super.init() // error! self.mediaId = mediaId self.description = description } }

Slide 134

Slide 134 text

class Image: Media { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } }

Slide 135

Slide 135 text

class Image: Media { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } func upload() -> () { println("Uploading…") } }

Slide 136

Slide 136 text

class Image: Media { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } override func upload() -> () { println("Uploading…") } }

Slide 137

Slide 137 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } func upload() -> () { println("Uploading…") } }

Slide 138

Slide 138 text

class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } final func upload() -> () { println("Uploading…") } }

Slide 139

Slide 139 text

final class Image { var mediaId :Int var description :String init(mediaId: Int, description: String) { self.mediaId = mediaId self.description = description super.init() // correct! } func upload() -> () { println("Uploading…") } }

Slide 140

Slide 140 text

class Image { var mediaId :Int var description :String //… func upload() -> () { println("Uploading…") } }

Slide 141

Slide 141 text

class Image { var mediaId :Int private var description :String //… private func upload() -> () { println("Uploading…") } }

Slide 142

Slide 142 text

private class Image { var mediaId :Int private var description :String //… private func upload() -> () { println("Uploading…") } }

Slide 143

Slide 143 text

internal class Image { var mediaId :Int internal var description :String //… private func upload() -> () { println("Uploading…") } }

Slide 144

Slide 144 text

Extensions

Slide 145

Slide 145 text

extension Image { }

Slide 146

Slide 146 text

extension Image { func supersize () -> (UIImage) { // do scaling here } }

Slide 147

Slide 147 text

Protocols

Slide 148

Slide 148 text

class Uploader { }

Slide 149

Slide 149 text

class Uploader { func upload(uploadee: Media) { } }

Slide 150

Slide 150 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } }

Slide 151

Slide 151 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } }

Slide 152

Slide 152 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } }

Slide 153

Slide 153 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } }

Slide 154

Slide 154 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } func doHTTPUpload(#source: String, #destination: String) -> () { // hand-waving… } }

Slide 155

Slide 155 text

class Media { var mediaId: Int = 200_000 var filePath: String = "/tmp/media.jpg" var fileType: String = "jpg" var fileName: String { return String(mediaId) + "." + fileType } func uploadInProgress() -> () { // do stuff } func uploadComplete() -> () { // do stuff } }

Slide 156

Slide 156 text

class Media { var mediaId: Int = 200_000 var filePath: String = "/tmp/media.jpg" var fileType: String = "jpg" var fileName: String { return String(mediaId) + "." + fileType } func uploadInProgress() -> () { // do stuff } func uploadComplete() -> () { // do stuff } }

Slide 157

Slide 157 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } func doHTTPUpload(#source: String, #destination: String) -> () { // hand-waving… } }

Slide 158

Slide 158 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } func doHTTPUpload(#source: String, #destination: String) -> () { // hand-waving… } }

Slide 159

Slide 159 text

protocol Uploadable { }

Slide 160

Slide 160 text

protocol Uploadable { var filePath: String { get } var fileName: String { get } }

Slide 161

Slide 161 text

protocol Uploadable { var filePath: String { get } var fileName: String { get } func uploadInProgress() -> () func uploadComplete() -> () }

Slide 162

Slide 162 text

class Uploader { func upload(uploadee: Media) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } func doHTTPUpload(#source: String, #destination: String) -> () { // hand-waving… } }

Slide 163

Slide 163 text

class Uploader { func upload(uploadee: Uploadable) { uploadee.uploadInProgress() doHTTPUpload(source: uploadee.filePath, destination: uploadee.fileName) uploadee.uploadComplete() } func doHTTPUpload(#source: String, #destination: String) -> () { // hand-waving… } }

Slide 164

Slide 164 text

class Media { var mediaId: Int = 200_000 var filePath: String = "/tmp/media.jpg" var fileType: String = "jpg" var fileName: String { return String(mediaId) + "." + fileType } func uploadInProgress() -> () { // do stuff } func uploadComplete() -> () { // do stuff } }

Slide 165

Slide 165 text

class Media: Uploadable { var mediaId: Int = 200_000 var filePath: String = "/tmp/media.jpg" var fileType: String = "jpg" var fileName: String { return String(mediaId) + "." + fileType } func uploadInProgress() -> () { // do stuff } func uploadComplete() -> () { // do stuff } }

Slide 166

Slide 166 text

class CSV: Uploadable { // … }

Slide 167

Slide 167 text

Structs and Enums

Slide 168

Slide 168 text

struct CGPoint { CGFloat x; CGFloat y; };

Slide 169

Slide 169 text

struct CGPoint { CGFloat x; CGFloat y; }; struct CGSize { CGFloat width; CGFloat height; };

Slide 170

Slide 170 text

struct CGPoint { CGFloat x; CGFloat y; }; struct CGSize { CGFloat width; CGFloat height; }; struct CGRect { CGPoint origin; CGSize size; };

Slide 171

Slide 171 text

struct CGPoint { CGFloat x; CGFloat y; }; struct CGSize { CGFloat width; CGFloat height; }; struct CGRect { CGPoint origin; CGSize size; }; 16.67

Slide 172

Slide 172 text

CGPointMake CGRectMake CGSizeMake CGRectDivide CGRectInset CGRectIntegral CGRectIntersection CGRectOffset CGRectUnion CGPointEqualToPoint CGSizeEqualToSize CGRectEqualToRect CGRectIntersectsRect CGRectContainsPoint CGRectContainsRect CGRectGetMinX CGRectGetMinY CGRectGetMidX CGRectGetMidY CGRectGetMaxX CGRectGetMaxY CGRectGetHeight CGRectGetWidth CGRectIsEmpty CGRectIsNull CGRectIsInfinite

Slide 173

Slide 173 text

struct Image { }

Slide 174

Slide 174 text

struct Image { var mediaId: Int var description: String }

Slide 175

Slide 175 text

struct Image { var mediaId: Int var description: String func upload() -> () { println("Uploading image \(self.mediaId)") } }

Slide 176

Slide 176 text

struct Image { var mediaId: Int var description: String func upload() -> () { println("Uploading image \(self.mediaId)") } } var myImage = Image(mediaId: 200_000, description: "test")

Slide 177

Slide 177 text

struct Image { var mediaId: Int var description: String func upload() -> () { println("Uploading image \(self.mediaId)") } } var myImage = Image(mediaId: 200_000, description: "test") myImage.upload()

Slide 178

Slide 178 text

struct Image { var mediaId: Int var description: String func upload() -> () { println("Uploading image \(self.mediaId)") } func bang() -> () { self.description.extend("!") } }

Slide 179

Slide 179 text

struct Image { var mediaId: Int var description: String func upload() -> () { println("Uploading image \(self.mediaId)") } mutating func bang() -> () { self.description.extend("!") } }

Slide 180

Slide 180 text

Should I use a struct or a class?

Slide 181

Slide 181 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct

Slide 182

Slide 182 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct imageStruct.description = "Kittens"

Slide 183

Slide 183 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct imageStruct.description = "Kittens" println(\(newImaegStruct.description)) // prints "Flowers"

Slide 184

Slide 184 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct imageStruct.description = "Kittens" println(\(newImaegStruct.description)) // prints "Flowers" var imageObject = imageObj(mediaId: 200_000, description: "Flowers") var newImageObject = imageObject

Slide 185

Slide 185 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct imageStruct.description = "Kittens" println(\(newImaegStruct.description)) // prints "Flowers" var imageObject = imageObj(mediaId: 200_000, description: "Flowers") var newImageObject = imageObject imageObject.description = "Kittens"

Slide 186

Slide 186 text

var imageStruct = imageStr(mediaId: 200_000, description: "Flowers") var newImageStruct = imageStruct imageStruct.description = "Kittens" println(\(newImaegStruct.description)) // prints "Flowers" var imageObject = imageObj(mediaId: 200_000, description: "Flowers") var newImageObject = imageObject imageObject.description = "Kittens" println(\(newImageObject.description)) // prints "Kittens"

Slide 187

Slide 187 text

Enums

Slide 188

Slide 188 text

enum Orientation { }

Slide 189

Slide 189 text

enum Orientation { case Portrait case Landscape case Square }

Slide 190

Slide 190 text

struct Image { var mediaId: Int var description: String }

Slide 191

Slide 191 text

struct Image { var mediaId: Int var description: String var orientation: Orientation }

Slide 192

Slide 192 text

struct Image { var mediaId: Int var description: String var orientation: Orientation } var myImage = Image( mediaId: 200_000, description: "Test", orientation: Orientation.Landscape )

Slide 193

Slide 193 text

struct Image { var mediaId: Int var description: String var orientation: Orientation } var myImage = Image( mediaId: 200_000, description: "Test", orientation: Orientation.Landscape )

Slide 194

Slide 194 text

struct Image { var mediaId: Int var description: String var orientation: Orientation } var myImage = Image( mediaId: 200_000, description: "Test", orientation: .Landscape )

Slide 195

Slide 195 text

enum Orientation { case Portrait case Landscape case Square }

Slide 196

Slide 196 text

enum Orientation { case Portrait case Landscape case Square func describe () -> () { } }

Slide 197

Slide 197 text

enum Orientation { case Portrait case Landscape case Square func describe () -> () { switch self { } } }

Slide 198

Slide 198 text

enum Orientation { case Portrait case Landscape case Square func describe () -> () { switch self { case .Portrait: println("Portrait orientation") case .Landscape: println("Landscape orientation") case .Square: println("What is this, Instagram?") } } }

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

struct Image { enum Orientation { case Portrait case Landscape case Square } var mediaId: Int var description: String var orientation: Orientation }

Slide 201

Slide 201 text

Generics

Slide 202

Slide 202 text

struct Lightbox { var items: [Image] mutating func addItem(item: Image) -> () { self.items.append(item) } mutating func merge(other: Lightbox) -> () { for item in other.items { self.addItem(item) } } }

Slide 203

Slide 203 text

struct Lightbox { var items: [Image] mutating func addItem(item: Image) -> () { self.items.append(item) } mutating func merge(other: Lightbox) -> () { for item in other.items { self.addItem(item) } } }

Slide 204

Slide 204 text

struct Lightbox { var items: [Image] mutating func addItem(item: Image) -> () { self.items.append(item) } mutating func merge(other: Lightbox) -> () { for item in other.items { self.addItem(item) } } }

Slide 205

Slide 205 text

struct Clipbox { var items: [Video] mutating func addItem(item: Video) -> () { self.items.append(item) } mutating func merge(other: Clipbox) -> () { for item in other.items { self.addItem(item) } } }

Slide 206

Slide 206 text

struct Clipbox { var items: [Video] mutating func addItem(item: Video) -> () { self.items.append(item) } mutating func merge(other: Clipbox) -> () { for item in other.items { self.addItem(item) } } }

Slide 207

Slide 207 text

struct Mediabox { }

Slide 208

Slide 208 text

struct Mediabox { var items: [T] }

Slide 209

Slide 209 text

struct Mediabox { var items: [T] = [] mutating func addItem(item: T) -> () { self.items.append(item) } mutating func merge(other: Mediabox) -> () { for item in other.items { self.addItem(item) } } }

Slide 210

Slide 210 text

var myLightbox = Mediabox() myLightbox.addItem(flowerImage) myLightbox.addItem(kittenImage) var myClipbox = Mediabox() myClipbox.addItem(surferVid) myClipbox.addItem(skierVid) myLightbox.addItem(surferVid) //error! myClipbox.merge(other: myLightbox) //error!

Slide 211

Slide 211 text

What Else?

Slide 212

Slide 212 text

@autoclosure automatic reference counting avoiding circular references with the weak keyword variadic parameters closed vs. open ranges associated and raw values for Enums where clauses in generics the Swift standard library operator overloading sequences and generators typealias type casting AnyObject calling Objective-C from Swift calling Swift from Objective-C

Slide 213

Slide 213 text

What's Missing?

Slide 214

Slide 214 text

Exceptions Options Meta-programming

Slide 215

Slide 215 text

What's Next?

Slide 216

Slide 216 text

No content

Slide 217

Slide 217 text

No content

Slide 218

Slide 218 text

No content

Slide 219

Slide 219 text

No content

Slide 220

Slide 220 text

developer.apple.com/swift/blog airspeedvelocity.net

Slide 221

Slide 221 text

No content