Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include ! int main() { printf("hello,...

343
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC14 Introduction to Swift Session 402 Tim Isted Developer Publications Engineer Tools Dave Addey Developer Publications Engineer

Transcript of Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include ! int main() { printf("hello,...

Page 1: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC14

Introduction to Swift

Session 402 Tim Isted Developer Publications Engineer

Tools

!

Dave Addey Developer Publications Engineer

Page 2: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")
Page 3: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")
Page 4: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

370,000iBooks Store downloads

Page 5: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

#include <stdio.h> !

int main() {     printf("hello, world\n"); return 0; }

println("hello, WWDC")

Page 6: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

#include <stdio.h> !

int main() {     printf("hello, world\n"); return 0; }

println("hello, WWDC")

Page 7: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What You Will Learn

Page 8: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What You Will Learn

Page 9: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What You Will Learn

Page 10: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What You Will Learn

Page 11: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

The Basics

Dave Addey Developer Publications Engineer

Page 12: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Variables

Page 13: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Variables

var

Page 14: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var languageName

Variables

Page 15: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var languageName:

Variables

Page 16: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var languageName: String

Variables

Page 17: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var languageName: String = "Swift"

Variables

Page 18: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let languageName: String = "Swift"

Variables

Page 19: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and

let languageName: String = "Swift"

Variables

Page 20: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and

let languageName: String = "Swift" var version: Double = 1.0

Variables

Page 21: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and

let languageName: String = "Swift" var version: Double = 1.0 var introduced: Int = 2014

Variables

Page 22: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and

let languageName: String = "Swift" var version: Double = 1.0 var introduced: Int = 2014 var isAwesome: Bool = true

Variables

Page 23: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and

let languageName: String = "Swift" var version: Double = 1.0 let introduced: Int = 2014 let isAwesome: Bool = true

Variables

Page 24: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and Variables

let languageName: String = "Swift" var version: Double = 1.0 let introduced: Int = 2014 let isAwesome: Bool = true

Page 25: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and Variables

let languageName: String = "Swift" var version: Double = 1.0 let introduced: Int = 2014 let isAwesome: Bool = true

Page 26: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Type Inference

let languageName = "Swift" // inferred as String var version = 1.0 // inferred as Double let introduced = 2014 // inferred as Int let isAwesome = true // inferred as Bool

Page 27: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let languageName = "Swift" var version = 1.0 let introduced = 2014 let isAwesome = true let π = 3.1415927

Unicode Names

Page 28: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let languageName = "Swift" var version = 1.0 let introduced = 2014 let isAwesome = true let π = 3.1415927let 🐶🐮 = "dogcow"

Unicode Names

Page 29: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

String

Page 30: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let someString = "I appear to be a string"

String

Page 31: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let someString = "I appear to be a string" // inferred to be of type String

String

Page 32: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let someString = "I appear to be a string" // inferred to be of type String !

urlRequest.HTTPMethod = "POST"

String

Page 33: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let someString = "I appear to be a string" // inferred to be of type String !

urlRequest.HTTPMethod = "POST" !

let components = "~/Documents/Swift".pathComponents

String

Page 34: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let someString = "I appear to be a string" // inferred to be of type String !

urlRequest.HTTPMethod = "POST" !

let components = "~/Documents/Swift".pathComponents // ["~", "Documents", "Swift"]

String

Page 35: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Character

Page 36: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

for character in "mouse" { println(character) }

Character

Page 37: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

for character in "mouse" { println(character) }

Character

mouse

Page 38: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

for character in "mús" { println(character) }

Character

m ú s

Page 39: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

for character in "мышь" { println(character) }

Character

м ы ш ь

Page 40: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

for character in "⿏鼠标" {

Character

⿏鼠 标

!

println(character) }

Page 41: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

!

println(character) }

for character in "🐭🐭🐭🐭🐭" {

Character

🐭 🐭 🐭 🐭 🐭

Page 42: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let dog: Character = "🐶"

Combining Strings and Characters

Page 43: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let dog: Character = "🐶" let cow: Character = "🐮"

Combining Strings and Characters

Page 44: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let dog: Character = "🐶" let cow: Character = "🐮" let dogCow = dog + cow // dogCow is "🐶🐮"

Combining Strings and Characters

Page 45: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let dog: Character = "🐶" let cow: Character = "🐮" let dogCow = dog + cow // dogCow is "🐶🐮" !

let instruction = "Beware of the " + dog // instruction is "Beware of the 🐶"

Combining Strings and Characters

Page 46: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Building Complex Strings

Page 47: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Building Complex Strings

let a = 3, b = 5

Page 48: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let a = 3, b = 5 !

// "3 times 5 is 15"

Building Complex Strings

Page 49: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let a = 3, b = 5 !

// "3 times 5 is 15" !

let mathResult = "\(a) times \(b) is \(a * b)"

String Interpolation

Page 50: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let a = 3, b = 5 !

// "3 times 5 is 15" !

let mathResult = "\(a) times \(b) is \(a * b)" !

// "3 times 5 is 15"

String Interpolation

Page 51: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let a = 7, b = 4 !

// "7 times 4 is 28" !

let mathResult = "\(a) times \(b) is \(a * b)" !

// "7 times 4 is 28"

String Interpolation

Page 52: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

String Mutability

Page 53: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"

String Mutability

Page 54: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"variableString += " and carriage"

String Mutability

Page 55: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

String Mutability

Page 56: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

String Mutability

let constantString = "Highlander"

Page 57: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

String Mutability

let constantString = "Highlander"constantString += " and another Highlander"

Page 58: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

String Mutability

let constantString = "Highlander"constantString += " and another Highlander"// error - constantString cannot be changed

Page 59: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary

Page 60: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary

let components = "~/Documents/Swift".pathComponents

Page 61: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary

let components = "~/Documents/Swift".pathComponents // ["~", "Documents", "Swift"] // returns an Array, not an NSArray

Page 62: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

Page 63: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

var names = ["Anna", "Alex", "Brian", "Jack"]

Page 64: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

var names = ["Anna", "Alex", "Brian", "Jack"]

Page 65: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

var names = ["Anna", "Alex", "Brian", "Jack"]

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 66: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

var names = ["Anna", "Alex", "Brian", "Jack"]

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 67: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Array and Dictionary Literals

var names = ["Anna", "Alex", "Brian", "Jack"]

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 68: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Arrays and Dictionaries

var names = ["Anna", "Alex", "Brian", "Jack"]

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 69: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Arrays and Dictionaries

var names = ["Anna", "Alex", "Brian", "Jack"]

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 70: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack"]

Page 71: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack"]

Page 72: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack", 42]

Page 73: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack", true]

Page 74: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack", Bicycle()]

Page 75: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack"]

Page 76: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names: String[] = ["Anna", "Alex", "Brian", "Jack"]

Page 77: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names: String[] = ["Anna", "Alex", "Brian", "Jack"]

Page 78: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names: String[] = ["Anna", "Alex", "Brian", "Jack"]

Page 79: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Typed Collections

var names = ["Anna", "Alex", "Brian", "Jack"] // an array of String values

Page 80: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var names = ["Anna", "Alex", "Brian", "Jack"] // an array of String values

Typed Collections

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 81: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var names = ["Anna", "Alex", "Brian", "Jack"] // an array of String values

Typed Collections

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Page 82: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var names = ["Anna", "Alex", "Brian", "Jack"] // an array of String values

Typed Collections

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] // a Dictionary with String keys and Int values

Page 83: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var names = ["Anna", "Alex", "Brian", "Jack"] // an array of String values

Typed Collections

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] // a Dictionary with String keys and Int values

Page 84: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Loops

Page 85: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Loops

while !sated { eatCake() } !

for var doctor = 1; doctor <= 13; ++doctor { exterminate(doctor) }

Page 86: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

🐭 🐭 🐭 🐭 🐭

For-In: Strings and Characters

for character in "🐭🐭🐭🐭🐭" {!

println(character) }

Page 87: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

For-In: Ranges

for number in 1...5 { println("\(number) times 4 is \(number * 4)") }

1 times 4 is 4 2 times 4 is 8 3 times 4 is 12 4 times 4 is 16 5 times 4 is 20

Page 88: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

For-In: Ranges

for number in 0..5 { println("\(number) times 4 is \(number * 4)") }

0 times 4 is 0 1 times 4 is 4 2 times 4 is 8 3 times 4 is 12 4 times 4 is 16

Page 89: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

For-In: Arrays

for name in ["Anna", "Alex", "Brian", "Jack"] { println("Hello, \(name)!") }

Hello, Anna! Hello, Alex! Hello, Brian! Hello, Jack!

Page 90: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

For-In: Dictionaries

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs") }

ants have 6 legs snakes have 0 legs cheetahs have 4 legs

Page 91: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

For-In: Dictionaries

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs") }

ants have 6 legs snakes have 0 legs cheetahs have 4 legs

Page 92: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Modifying an Array

Page 93: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"]

Modifying an Array

Page 94: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"]

Modifying an Array

["Eggs", "Milk"]

Page 95: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0])

Modifying an Array

Page 96: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0])

Modifying an Array

"Eggs"

Page 97: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour"

Modifying an Array

Page 98: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour"

Modifying an Array

["Eggs", "Milk", "Flour"]

Page 99: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"]

Modifying an Array

Page 100: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"]

Modifying an Array

["Eggs", "Milk", "Flour", "Cheese", "Butter","Chocolate Spread"]

Page 101: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"] shoppingList[0] = "Six eggs"

Modifying an Array

["Eggs", "Milk", "Flour", "Cheese", "Butter", "Chocolate Spread"]

Page 102: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"] shoppingList[0] = "Six eggs"

Modifying an Array

["Six eggs", "Milk", "Flour", "Cheese", "Butter", "Chocolate Spread"]

Page 103: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"] shoppingList[0] = "Six eggs" shoppingList[3...5] = ["Bananas", "Apples"]

Modifying an Array

["Six eggs", "Milk", "Flour", "Cheese", "Butter", "Chocolate Spread"]

Page 104: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var shoppingList = ["Eggs", "Milk"] println(shoppingList[0]) shoppingList += "Flour" shoppingList += ["Cheese", "Butter", "Chocolate Spread"] shoppingList[0] = "Six eggs" shoppingList[3...5] = ["Bananas", "Apples"]

Modifying an Array

["Six eggs", "Milk", "Flour", "Bananas", "Apples"]

Page 105: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Modifying a Dictionary

Page 106: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Modifying a Dictionary

Page 107: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Modifying a Dictionary

["ant": 6, "snake": 0, "cheetah": 4]

Page 108: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] numberOfLegs["spider"] = 273

Modifying a Dictionary

Page 109: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] numberOfLegs["spider"] = 273

Modifying a Dictionary

["ant": 6, "snake": 0, "cheetah": 4, "spider": 273]

Page 110: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] numberOfLegs["spider"] = 273 numberOfLegs["spider"] = 8

Modifying a Dictionary

["ant": 6, "snake": 0, "cheetah": 4, "spider": 273]

Page 111: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] numberOfLegs["spider"] = 273 numberOfLegs["spider"] = 8

Modifying a Dictionary

["ant": 6, "snake": 0, "cheetah": 4, "spider": 8]

Page 112: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

Retrieving a Value from a Dictionary

Page 113: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] // aardvark?

Retrieving a Value from a Dictionary

Page 114: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] // aardvark? // dugong?

Retrieving a Value from a Dictionary

Page 115: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] // aardvark? // dugong? // venezuelan poodle moth?

Retrieving a Value from a Dictionary

???????

Page 116: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Beyond the Basics

Tim Isted Developer Publications Engineer

Page 117: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

let possibleLegCount = numberOfLegs["aardvark"]

Retrieving a Value from a Dictionary

Page 118: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

let possibleLegCount: Int? = numberOfLegs["aardvark"]

Optionals

Page 119: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

let possibleLegCount: Int? = numberOfLegs["aardvark"] !

if possibleLegCount == nil { println("Aardvark wasn't found") }

Querying an Optional

Page 120: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

let possibleLegCount: Int? = numberOfLegs["aardvark"] !

if possibleLegCount == nil { println("Aardvark wasn't found") } else { let legCount = possibleLegCount! println("An aardvark has \(legCount) legs") }

Querying an Optional

Page 121: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

let possibleLegCount: Int? = numberOfLegs["aardvark"] !

if possibleLegCount == nil { println("Aardvark wasn't found") } else { let legCount: Int = possibleLegCount! println("An aardvark has \(legCount) legs") }

Querying an Optional

Page 122: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

if possibleLegCount { let legCount = possibleLegCount! !

println("An aardvark has \(legCount) legs") }

Querying an Optional

Page 123: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

if let legCount = possibleLegCount { println("An aardvark has \(legCount) legs") }

Unwrapping an Optional

Page 124: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

if let legCount = possibleLegCount { println("An aardvark has \(legCount) legs") }

Unwrapping an Optional

Page 125: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

If Statements

if legCount == 0 { println("It slithers and slides around") } else { println("It walks") }

Page 126: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

If Statements

if (legCount == 0) { println("It slithers and slides around") } else { println("It walks") }

Page 127: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

If Statements

if legCount == 0 { println("It slithers and slides around") } else { println("It walks") }

Page 128: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

If Statements

if legCount == 0 { println("It slithers and slides around") } else { println("It walks") }

Page 129: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

More Complex If Statements

if legCount == 0 { println("It slithers and slides around") } else if legCount == 1 { println("It hops") } else { println("It walks") }

Page 130: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch legCount { case 0: println("It slithers and slides around") !

case 1: println("It hops") !

default: println("It walks") }

Page 131: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch sender { case executeButton: println("You tapped the Execute button") !

case firstNameTextField: println("You tapped the First Name text field") !

default: println("You tapped some other object") }

Page 132: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch legCount { case 0: println("It slithers and slides around") !

case 1, 3, 5, 7, 9, 11, 13: println("It limps") !

case 2, 4, 6, 8, 10, 12, 14: println("It walks") }

Page 133: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch legCount { case 0: println("It slithers and slides around") !

case 1, 3, 5, 7, 9, 11, 13: println("It limps") !

case 2, 4, 6, 8, 10, 12, 14: println("It walks") } // error: switch must be exhaustive

Page 134: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch legCount { case 0: println("It slithers and slides around") !

case 1, 3, 5, 7, 9, 11, 13: println("It limps") !

default: println("It walks") }

Page 135: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Switch

switch legCount { case 0: println("It slithers and slides around") !

case 1, 3, 5, 7, 9, 11, 13: println("It limps") !

default: println("It walks") }

Page 136: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Matching Value Ranges

switch legCount { case 0: println("It has no legs") !

case 1...8: println("It has a few legs") !

default: println("It has lots of legs") }

Page 137: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Matching Value Ranges

switch legCount { case 0: println("It has no legs") !

case 1...8: println("It has a few legs") !

default: println("It has lots of legs") }

Page 138: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Matching Value Ranges

switch legCount { case 0: println("It has no legs") !

case 1...8: println("It has a few legs") !

default: println("It has lots of legs") }

• Intermediate Swift Presidio Wednesday 2:00PM

Page 139: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Matching Value Ranges

switch legCount { case 0: println("It has no legs") !

case 1...8: println("It has a few legs") !

default: println("It has lots of legs") }

Page 140: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions

func sayHello() { println("Hello!") }

Page 141: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions

func sayHello() { println("Hello!") } !

sayHello()

Page 142: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions

func sayHello() { println("Hello!") } !

sayHello()

Hello

Page 143: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions with Parameters

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

Page 144: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions with Parameters

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

sayHello("WWDC")

Page 145: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Functions with Parameters

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

sayHello("WWDC")

Hello WWDC!

Page 146: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Parameter Values

func sayHello(name: String = "World") { println("Hello \(name)!") } !

Page 147: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Parameter Values

func sayHello(name: String = "World") { println("Hello \(name)!") } !

sayHello()

Page 148: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Parameter Values

func sayHello(name: String = "World") { println("Hello \(name)!") } !

sayHello()

Hello World!

Page 149: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Parameter Values

func sayHello(name: String = "World") { println("Hello \(name)!") } !

sayHello() sayHello(name: "WWDC")

Hello World!

Page 150: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Parameter Values

func sayHello(name: String = "World") { println("Hello \(name)!") } !

sayHello() sayHello(name: "WWDC")

Hello World!

Hello WWDC!

Page 151: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Values

func buildGreeting(name: String = "World") -> String { return "Hello " + name }

Page 152: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Values

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

let greeting = buildGreeting()

Page 153: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Values

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

let greeting: String = buildGreeting()

Page 154: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Values

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

let greeting = buildGreeting() !

println(greeting)

Page 155: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Values

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

let greeting = buildGreeting() !

println(greeting)

Hello World

Page 156: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Multiple Values

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") }

Page 157: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Multiple Values

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") }

Page 158: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Tuples

(3.79, 3.99, 4.19) !

(404, "Not found") !

(2, "banana", 0.72)

Page 159: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Tuples

(3.79, 3.99, 4.19) // (Double, Double, Double) !

(404, "Not found") // (Int, String) !

(2, "banana", 0.72) // (Int, String, Double)

Page 160: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Returning Multiple Values

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") }

Page 161: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Decomposing a Tuple

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") } !

let (statusCode, message) = refreshWebPage()

Page 162: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Decomposing a Tuple

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") } !

let (statusCode, message) = refreshWebPage() !

println("Received \(statusCode): \(message)")

Page 163: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Decomposing a Tuple

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") } !

let (statusCode, message) = refreshWebPage() !

println("Received \(statusCode): \(message)")

Received 200: Success

Page 164: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Decomposing a Tuple

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") } !

let (statusCode: Int, message: String) = refreshWebPage() !

println("Received \(statusCode): \(message)")

Received 200: Success

Page 165: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Tuple Decomposition for Enumeration

let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] !

for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs") }

ants have 6 legs snakes have 0 legs cheetahs have 4 legs

Page 166: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Named Values in a Tuple

func refreshWebPage() -> (Int, String) { // ...try to refresh... return (200, "Success") }

Page 167: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Named Values in a Tuple

func refreshWebPage() -> (code: Int, message: String) { // ...try to refresh... return (200, "Success") }

Page 168: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Named Values in a Tuple

func refreshWebPage() -> (code: Int, message: String) { // ...try to refresh... return (200, "Success") } !

let status = refreshWebPage() !

println("Received \(status.code): \(status.message)")

Page 169: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Named Values in a Tuple

func refreshWebPage() -> (code: Int, message: String) { // ...try to refresh... return (200, "Success") } !

let status = refreshWebPage() !

println("Received \(status.code): \(status.message)")

Received 200: Success

Page 170: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter = { println("Hello World!") }

Page 171: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter: () -> () = { println("Hello World!") }

Page 172: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter: () -> () = { println("Hello World!") } !

func greetingPrinter() -> () { println("Hello World!") }

Page 173: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter: () -> () = { println("Hello World!") } !

func greetingPrinter() -> () { println("Hello World!") }

Page 174: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter: () -> () = { println("Hello World!") } !

greetingPrinter()

Page 175: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures

let greetingPrinter: () -> () = { println("Hello World!") } !

greetingPrinter()

Hello World!

Page 176: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures as Parameters

func repeat(count: Int, task: () -> ()) { for i in 0..count { task() } }

Page 177: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures as Parameters

func repeat(count: Int, task: () -> ()) { for i in 0..count { task() } } repeat(2, { println("Hello!") })

Page 178: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Closures as Parameters

func repeat(count: Int, task: () -> ()) { for i in 0..count { task() } } repeat(2, { println("Hello!") })

Hello!Hello!

Page 179: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Trailing Closures

func repeat(count: Int, task: () -> ()) { for i in 0..count { task() } } repeat(2) { println("Hello!") }

Hello!Hello!

Page 180: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Trailing Closures

func repeat(count: Int, task: () -> ()) { for i in 0..count { task() } } repeat(2) { println("Hello!") }

Hello!Hello!

Page 181: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

Dave Addey Developer Publications Engineer

Page 182: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

Page 183: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { !

!

!

}

Page 184: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { !

!

!

}

Page 185: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { !

!

!

}

Page 186: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { // properties !

!

}

Page 187: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { // properties // methods !

}

Page 188: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { // properties // methods // initializers }

Page 189: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

import "Vehicle.h" !

class Vehicle { }

Page 190: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

import "Vehicle.h" !

class Vehicle { }

Page 191: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { }

Page 192: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle: ???????? { }

Page 193: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle: ???????? { }

Page 194: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle: NSObject { }

Page 195: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Classes

class Vehicle { }

Page 196: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Inheritance

class Vehicle { !

} !

class Bicycle: Vehicle { !

}

Page 197: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Inheritance

class Vehicle { !

} !

class Bicycle: Vehicle { !

}

Page 198: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Inheritance

class Vehicle { !

} !

class Bicycle: Vehicle { !

}

Page 199: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Inheritance

class Vehicle { !

}

Page 200: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Properties

class Vehicle { var numberOfWheels = 0 }

Page 201: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Properties

class Vehicle { var numberOfWheels = 0 }

Page 202: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Properties

class Vehicle { let numberOfWheels = 0 }

Page 203: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Properties

class Vehicle { var numberOfWheels = 0 }

Page 204: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Properties

class Vehicle { var numberOfWheels = 0 }

Page 205: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

PropertiesStored

class Vehicle { var numberOfWheels = 0 }

Page 206: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

PropertiesStored

class Vehicle { var numberOfWheels = 0 }

Page 207: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

Page 208: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } } }

Page 209: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } } }

Page 210: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } } }

Page 211: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } } }

Page 212: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } set { } } }

Page 213: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { get { return "\(numberOfWheels) wheels" } } }

Page 214: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } }

Page 215: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Computed Properties

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } }

Page 216: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Initializer Syntax

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } !

let someVehicle = Vehicle()

Page 217: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } !

let someVehicle = Vehicle()

Initializer Syntax

Page 218: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } !

let someVehicle = Vehicle()

Automatic Memory Allocation

Page 219: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Type Inference

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } !

let someVehicle: Vehicle = Vehicle()

Page 220: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Default Values

class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } !

let someVehicle = Vehicle()

Page 221: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle()

Page 222: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle() !

println(someVehicle.description)

Page 223: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle() !

println(someVehicle.description) // 0 wheels

Page 224: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle() !

println(someVehicle.description) // 0 wheels !

someVehicle.numberOfWheels = 2

Page 225: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle() !

println(someVehicle.description) // 0 wheels !

someVehicle.numberOfWheels = 2 !

println(someVehicle.description)

Page 226: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Dot Syntax

let someVehicle = Vehicle() !

println(someVehicle.description) // 0 wheels !

someVehicle.numberOfWheels = 2 !

println(someVehicle.description) // 2 wheels

Page 227: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

class Bicycle: Vehicle { !

!

!

!

}

Class Initialization

Page 228: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { !

} }

Page 229: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { !

} }

Page 230: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() } }

Page 231: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 } }

Page 232: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 } }

• Intermediate Swift Presidio Wednesday 2:00PM

Page 233: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 } } !

let myBicycle = Bicycle()

Page 234: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 } } !

let myBicycle = Bicycle() println(myBicycle.description)

Page 235: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Class Initialization

class Bicycle: Vehicle { init() { super.init() numberOfWheels = 2 } } !

let myBicycle = Bicycle() println(myBicycle.description) // 2 wheels

Page 236: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

Page 237: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { !

!

!

!

!

!

!

!

}

Page 238: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 // inferred as Double !

!

!

!

!

!

!

}

Page 239: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } !

!

!

}

Page 240: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } var description: String { } }

Page 241: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } override var description: String { } }

Page 242: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } override var description: String { } }

Page 243: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } override var description: String { return super.description + ", \(speed) mph" } }

Page 244: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

class Car: Vehicle { var speed = 0.0 init() { super.init() numberOfWheels = 4 } override var description: String { return super.description + ", \(speed) mph" } }

Page 245: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

let myCar = Car()

Page 246: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

let myCar = Car() !

println(myCar.description) // 4 wheels, 0.0 mph

Page 247: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

let myCar = Car() !

println(myCar.description) // 4 wheels, 0.0 mph !

myCar.speed = 35.0

Page 248: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Overriding a Property

let myCar = Car() !

println(myCar.description) // 4 wheels, 0.0 mph !

myCar.speed = 35.0 !

println(myCar.description) // 4 wheels, 35.0 mph

Page 249: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

Page 250: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { !

!

!

!

!

!

!

!

}

Page 251: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { !

!

!

!

!

!

} }

Page 252: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { } didSet { } } }

Page 253: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { // newValue is available here } didSet { } } }

Page 254: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { } didSet { // oldValue is available here } } }

Page 255: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { !

!

} } }

Page 256: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { if newValue > 65.0 { } } } }

Page 257: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Property Observers

class ParentsCar: Car { override var speed: Double { willSet { if newValue > 65.0 { println("Careful now.") } } } }

Page 258: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

Page 259: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 !

!

}

Page 260: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 !

!

}

Page 261: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 func increment() { count++ } }

Page 262: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 func incrementBy(amount: Int) { count += amount } }

Page 263: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 func incrementBy(amount: Int) { count += amount } }

Page 264: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Methods

class Counter { var count = 0 func incrementBy(amount: Int) { count += amount } func resetToCount(count: Int) { self.count = count } }

Page 265: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Beyond Classes

Tim Isted Developer Publications Engineer

Page 266: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

Page 267: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

struct Point { var x, y: Double }

struct Size { var width, height: Double }

struct Rect { var origin: Point var size: Size }

Page 268: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

var point = Point(x: 0.0, y: 0.0) !

var size = Size(width: 640.0, height: 480.0) !

var rect = Rect(origin: point, size: size)

Page 269: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

var point = Point(x: 0.0, y: 0.0) !

var size = Size(width: 640.0, height: 480.0) !

var rect = Rect(origin: point, size: size)

• Intermediate Swift Presidio Wednesday 2:00PM

Page 270: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

struct Rect { var origin: Point var size: Size }

Page 271: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

struct Rect { var origin: Point var size: Size !

var area: Double { return size.width * size.height } }

Page 272: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

struct Rect { var origin: Point var size: Size !

var area: Double { return size.width * size.height } !

func isBiggerThanRect(other: Rect) -> Bool { return self.area > other.area } }

Page 273: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures in Swift

struct Rect { var origin: Point var size: Size !

var area: Double { return size.width * size.height } !

func isBiggerThanRect(other: Rect) -> Bool { return self.area > other.area } }

Page 274: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures and Classes

struct Rect { var origin: Point var size: Size !

var area: Double { return size.width * size.height } } !

class Window { var frame: Rect ... }

Page 275: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window

Structure or Class?

= Window(frame: frame)

Page 276: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window =

Structure or Class?

Window

Window(frame: frame)

Page 277: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window =

Structure or Class?

Window

Window(frame: frame)

Page 278: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window

setup(window)

What if…

Window

Window(frame: frame)

Window

Window(frame: frame)

Page 279: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window

setup(window)

What if…

Window

Window(frame: frame)Window

Window(frame: frame)

Page 280: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window

setup(window)

What if…

Window

Window(frame: frame)Window

Window(frame: frame)

Page 281: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

var window

setup(window)

func setup(window: Window) { // do some setup }

Class Instances are Passed by Reference

Window

Window(frame: frame)

Page 282: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What if…

Window

var newFrame = window.frame

Page 283: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What if…

var newFrame = window.frame !

newFrame.origin.x = 20.0

Window

Page 284: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

What if…

var newFrame = window.frame !

newFrame.origin.x = 20.0

Window

Page 285: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Structures are Passed by Value

var newFrame = window.frame !

newFrame.origin.x = 20.0

Window

Page 286: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window

Constants and Variables: Reference Types

= Window(frame: frame)

Page 287: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window =

Constants and Variables: Reference Types

Window

Window(frame: frame)

Page 288: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window =

Constants and Variables: Reference Types

Window

Window(frame: frame)

Page 289: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window

window.title = "Hello!"

Constants and Variables: Reference Types

Window

Window(frame: frame)

Window

Window(frame: frame)

Page 290: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window

window.title = "Hello!"

Constants and Variables: Reference Types

Window

Window(frame: frame)

Hello!

Window(frame: frame)

Page 291: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window

window.title = "Hello!" !

!

!

!

window = Window(frame: frame)

Constants and Variables: Reference Types

Window

Window(frame: frame)

Hello!

Window(frame: frame)

Page 292: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

let window

window.title = "Hello!" !

!

!

!

window = Window(frame: frame) // error: Cannot mutate a constant!

Constants and Variables: Reference Types

Window

Window(frame: frame)

Hello!

Window(frame: frame)

Page 293: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

Page 294: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 0.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

Page 295: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 0.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

!

!

point1.x = 5

Page 296: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 5.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

!

!

point1.x = 5

Page 297: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 0.0 y: 0.0

x: 5.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

!

!

point1.x = 5 !

!

let point2 = Point(x: 0.0, y: 0.0)

Page 298: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 0.0 y: 0.0

x: 5.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

!

!

point1.x = 5 !

!

let point2 = Point(x: 0.0, y: 0.0) !

!

!

point2.x = 5

Page 299: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

x: 0.0 y: 0.0

x: 5.0 y: 0.0

Constants and Variables: Value Types

var point1 = Point(x: 0.0, y: 0.0) !

!

!

point1.x = 5 !

!

let point2 = Point(x: 0.0, y: 0.0) !

!

!

point2.x = 5 // error: Cannot mutate a constant!

Page 300: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Mutating a Structure

struct Point { var x, y: Double }

Page 301: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Mutating a Structure

struct Point { var x, y: Double !

func moveToTheRightBy(dx: Double) { x += dx } }

Page 302: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Mutating a Structure

struct Point { var x, y: Double !

mutating func moveToTheRightBy(dx: Double) { x += dx } }

Page 303: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Mutating a Structure

struct Point { var x, y: Double !

mutating func moveToTheRightBy(dx: Double) { x += dx } } !

let point = Point(x: 0.0, y: 0.0) point.moveToTheRightBy(200.0) // Error: Cannot mutate a constant!

Page 304: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Raw Values

enum Planet: Int { case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune }

Page 305: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Raw Values

enum Planet: Int { case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune } !

let earthNumber = Planet.Earth.toRaw() // earthNumber is 3

Page 306: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Raw Values

enum ControlCharacter: Character { case Tab = "\t" case Linefeed = "\n" case CarriageReturn = "\r" }

Page 307: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

enum CompassPoint { case North, South, East, West }

Page 308: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

enum CompassPoint { case North, South, East, West } !

var directionToHead = CompassPoint.West // directionToHead is inferred to be a CompassPoint

Page 309: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

enum CompassPoint { case North, South, East, West } !

var directionToHead = CompassPoint.West // directionToHead is inferred to be a CompassPoint directionToHead = .East

Page 310: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

enum CompassPoint { case North, South, East, West } !

var directionToHead = CompassPoint.West // directionToHead is inferred to be a CompassPoint directionToHead = .East !

let label = UILabel() label.textAlignment = .Right

Page 311: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Associated Values

enum TrainStatus { case OnTime case Delayed(Int) }

Page 312: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Associated Values

enum TrainStatus { case OnTime case Delayed(Int) }

Page 313: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Associated Values

enum TrainStatus { case OnTime case Delayed(Int) } !

var status = TrainStatus.OnTime // status is inferred to be a TrainStatus

Page 314: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Associated Values

enum TrainStatus { case OnTime case Delayed(Int) } !

var status = TrainStatus.OnTime // status is inferred to be a TrainStatus !

status = .Delayed(42)

Page 315: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

enum TrainStatus { case OnTime, Delayed(Int) !

!

!

!

!

!

!

!

!

!

!

}

Page 316: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Initializers

enum TrainStatus { case OnTime, Delayed(Int) init() { self = OnTime } !

!

!

!

!

!

!

}

Page 317: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations: Properties

enum TrainStatus { case OnTime, Delayed(Int) init() { self = OnTime } var description: String { switch self { case OnTime: return "on time" case Delayed(let minutes): return "delayed by \(minutes) minute(s)" } } }

Page 318: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

var status = TrainStatus()

Page 319: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

var status = TrainStatus() !

println("The train is \(status.description)") // The train is on time

Page 320: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

var status = TrainStatus() !

println("The train is \(status.description)") // The train is on time !

status = .Delayed(42)

Page 321: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Enumerations

var status = TrainStatus() !

println("The train is \(status.description)") // The train is on time !

status = .Delayed(42) !

println("The train is now \(status.description)") // The train is now delayed by 42 minute(s)

Page 322: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Nested Types

class Train { enum Status { case OnTime, Delayed(Int) init() { self = OnTime } var description: String { ... } } var status = Status() }

Page 323: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

Page 324: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension Size { mutating func increaseByFactor(factor: Int) { width *= factor height *= factor } }

Page 325: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension CGSize { mutating func increaseByFactor(factor: Int) { width *= factor height *= factor } }

Page 326: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension Int { !

!

!

!

!

}

Page 327: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension Int { func repetitions(task: () -> ()) { for i in 0..self { task() } } }

Page 328: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension Int { func repetitions(task: () -> ()) { for i in 0..self { task() } } } !

500.repetitions({ println("Hello!") })

Page 329: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Extensions

extension Int { func repetitions(task: () -> ()) { for i in 0..self { task() } } } !

500.repetitions { println("Hello!") }

Page 330: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Non-Generic Stack Structure

struct IntStack { var elements = Int[]() !

mutating func push(element: Int) { elements.append(element) } !

mutating func pop() -> Int { return elements.removeLast() } }

Page 331: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Non-Generic Stack Structure

struct IntStack { var elements = Int[]() !

mutating func push(element: Int) { elements.append(element) } !

mutating func pop() -> Int { return elements.removeLast() } }

Page 332: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Generic Stack Structure

struct Stack<T> { var elements = T[]() !

mutating func push(element: T) { elements.append(element) } !

mutating func pop() -> T { return elements.removeLast() } }

Page 333: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Generic Stack Structure

struct Stack<T> { var elements = T[]() !

mutating func push(element: T) { elements.append(element) } !

mutating func pop() -> T { return elements.removeLast() } }

Page 334: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Generic Stack Structure

struct Stack<T> { ... } !

var intStack = Stack<Int>() intStack.push(50) let lastIn = intStack.pop()

Page 335: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Generic Stack Structure

struct Stack<T> { ... } !

var intStack = Stack<Int>() intStack.push(50) let lastIn = intStack.pop() !

var stringStack = Stack<String>() stringStack.push("Hello") println(stringStack.pop())

Page 336: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

A Generic Stack Structure

struct Stack<T> { ... } !

var intStack = Stack<Int>() intStack.push(50) let lastIn = intStack.pop() !

var stringStack = Stack<String>() stringStack.push("Hello") println(stringStack.pop())

• Advanced Swift Presidio Thursday 11:30AM

Page 337: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Resources

Page 338: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Resources

Page 339: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Resources

Page 340: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Resources

• Intermediate Swift Presidio Wednesday 2:00PM

• Advanced Swift Presidio Thursday 11:30AM

Page 341: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

Resources

• Intermediate Swift Presidio Wednesday 2:00PM

• Advanced Swift Presidio Thursday 11:30AM

• Integrating Swift with Objective-C Presidio Wednesday 9:00AM

• Swift Interoperability in Depth Presidio Wednesday 3:15PM

Page 342: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")

More Information

Dave DeLong Developer Tools Evangelist [email protected]

Documentation The Swift Programming Language Using Swift with Cocoa and Objective-C http://developer.apple.com

Apple Developer Forums http://devforums.apple.com

Page 343: Introduction to Swift - Apple Inc. · 2016. 7. 8. · #include  ! int main() { printf("hello, world\n"); return 0; } println(" WWDC")