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
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…")
}
}
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
}
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