3. @genehack › Intro to Swift › TPC 2017
Hi I'm John
aka @genehack
• VP Tech, Infinity Interactive
• Perl tribe
• Polyglot coder
• Just this guy, you know?
37. @genehack › Intro to Swift › TPC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
38. @genehack › Intro to Swift › TPC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
39. @genehack › Intro to Swift › TPC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
40. @genehack › Intro to Swift › TPC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
41. @genehack › Intro to Swift › TPC 2017
Variables
let bar = 1
bar += 1
// ^^ compile time error!
42. @genehack › Intro to Swift › TPC 2017
Variables
let bar = 1
bar += 1
// ^^ compile time error!
43. @genehack › Intro to Swift › TPC 2017
Variables
let bar
// also a compile time error
/*
You canNOT have an uninitialized and
untyped variable. You also can't use
an uninitialized variable _at all_
*/
44. @genehack › Intro to Swift › TPC 2017
Variables
let bar
// also a compile time error
/*
You canNOT have an uninitialized and
untyped variable. You also can't use
an uninitialized variable _at all_
*/
47. @genehack › Intro to Swift › TPC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
48. @genehack › Intro to Swift › TPC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
49. @genehack › Intro to Swift › TPC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
50. @genehack › Intro to Swift › TPC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
51. @genehack › Intro to Swift › TPC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
52. @genehack › Intro to Swift › TPC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
53. @genehack › Intro to Swift › TPC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
54. @genehack › Intro to Swift › TPC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
55. @genehack › Intro to Swift › TPC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
56. @genehack › Intro to Swift › TPC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
57. @genehack › Intro to Swift › TPC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
58. @genehack › Intro to Swift › TPC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
59. @genehack › Intro to Swift › TPC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
60. @genehack › Intro to Swift › TPC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown -= 1
}
61. @genehack › Intro to Swift › TPC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown -= 1
}
62. @genehack › Intro to Swift › TPC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
63. @genehack › Intro to Swift › TPC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
64. @genehack › Intro to Swift › TPC 2017
Flow Control
var countUp = 0
repeat {
countUp += 1
} while countUp < 5
65. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
66. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
67. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
68. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
No fallthru by default!
69. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
70. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = "foo"
switch sample {
case "foo":
print("Is foo")
case "bar":
print("Is bar")
default: // mandatory when cases not exclusive
print("Not foo or bar, is it.")
}
71. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = "foo"
switch sample {
case "foo":
print("Is foo")
case "bar":
print("Is bar")
default: // mandatory when cases not exclusive
print("Not foo or bar, is it.")
}
72. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
73. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
74. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
75. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
76. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 3):
print("Is foo, 3")
case (let one, let two):
print("Is (one) and (two)")
}
77. @genehack › Intro to Swift › TPC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 3):
print("Is foo, 3")
case (let one, let two):
print("Is (one) and (two)")
}
78. @genehack › Intro to Swift › TPC 2017
Strings
var myString = "this is a string"
if myString.isEmpty {
// do something
}
myString += "and this is a longer string"
79. @genehack › Intro to Swift › TPC 2017
Strings
var myString = "this is a string"
if myString.isEmpty {
// do something
}
myString += "and this is a longer string"
80. @genehack › Intro to Swift › TPC 2017
Strings
var myString = "this is a string"
if myString.isEmpty {
// do something
}
myString += "and this is a longer string"
82. @genehack › Intro to Swift › TPC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
83. @genehack › Intro to Swift › TPC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
84. @genehack › Intro to Swift › TPC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
85. @genehack › Intro to Swift › TPC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
86. @genehack › Intro to Swift › TPC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
87. @genehack › Intro to Swift › TPC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
88. @genehack › Intro to Swift › TPC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
89. @genehack › Intro to Swift › TPC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
90. @genehack › Intro to Swift › TPC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
91. @genehack › Intro to Swift › TPC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
92. @genehack › Intro to Swift › TPC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
93. @genehack › Intro to Swift › TPC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
94. @genehack › Intro to Swift › TPC 2017
Optional Types
// unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
95. @genehack › Intro to Swift › TPC 2017
Optional Types
// unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
96. @genehack › Intro to Swift › TPC 2017
Optional Types
// unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
97. @genehack › Intro to Swift › TPC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
98. @genehack › Intro to Swift › TPC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
99. @genehack › Intro to Swift › TPC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
100. @genehack › Intro to Swift › TPC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
101. @genehack › Intro to Swift › TPC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
102. @genehack › Intro to Swift › TPC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
104. @genehack › Intro to Swift › TPC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
105. @genehack › Intro to Swift › TPC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
106. @genehack › Intro to Swift › TPC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
107. @genehack › Intro to Swift › TPC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
108. @genehack › Intro to Swift › TPC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
109. @genehack › Intro to Swift › TPC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
110. @genehack › Intro to Swift › TPC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
111. @genehack › Intro to Swift › TPC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
112. @genehack › Intro to Swift › TPC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
113. @genehack › Intro to Swift › TPC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
114. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": "Virginia",
]
// capitalCityStates has type
// [String:String]
115. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": "Virginia",
]
// capitalCityStates has type
// [String:String]
116. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": "Virginia",
]
// capitalCityStates has type
// [String:String]
117. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": 2,
]
// capitalCityStates must be
// annotated with type
// [String:Any]
118. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": 2,
]
// capitalCityStates must be
// annotated with type
// [String:Any]
119. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Richmond": 2,
]
// capitalCityStates must be
// annotated with type
// [String:Any]
120. @genehack › Intro to Swift › TPC 2017
Dictionary
let capitalCityStates: [String:Any] = [
"Salem": "Oregon",
"Richmond": 2,
]
// capitalCityStates must be
// annotated with type [String:Any]
121. @genehack › Intro to Swift › TPC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
122. @genehack › Intro to Swift › TPC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
123. @genehack › Intro to Swift › TPC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
124. @genehack › Intro to Swift › TPC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
150. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
151. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
152. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
153. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
This is already the func sig for sort!
154. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// inferred param & return types
var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1})
// sorted = [1, 2, 13, 32, 56, 120]
155. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// inferred param & return types
var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1})
// sorted = [1, 2, 13, 32, 56, 120]
156. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// positionally named parameters
var sorted = numbers.sorted(by: {return $0 > $1})
// sorted = [1, 2, 13, 32, 56, 120]
157. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// positionally named parameters
var sorted = numbers.sorted(by: {return $0 > $1})
// sorted = [1, 2, 13, 32, 56, 120]
158. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// when closure is last param,
// param name & parens optional
var sorted = numbers.sorted { $0 > $1 }
// sorted = [1, 2, 13, 32, 56, 120]
159. @genehack › Intro to Swift › TPC 2017
Closures
let numbers = [2,1,56,32,120,13]
// when closure is last param,
// param name & parens optional
var sorted = numbers.sorted { $0 > $1 }
// sorted = [1, 2, 13, 32, 56, 120]
162. @genehack › Intro to Swift › TPC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
163. @genehack › Intro to Swift › TPC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
164. @genehack › Intro to Swift › TPC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
Class 'Dog' has no initializers
165. @genehack › Intro to Swift › TPC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
166. @genehack › Intro to Swift › TPC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
167. @genehack › Intro to Swift › TPC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
168. @genehack › Intro to Swift › TPC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
169. @genehack › Intro to Swift › TPC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
170. @genehack › Intro to Swift › TPC 2017
Deinitializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
deinit () {
// do any cleanup here
}
}
171. @genehack › Intro to Swift › TPC 2017
Deinitializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
deinit () {
// do any cleanup here
}
}
172. @genehack › Intro to Swift › TPC 2017
Methods
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
func speak () -> String {
return self.noise
}
}
173. @genehack › Intro to Swift › TPC 2017
Methods
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
func speak () -> String {
return self.noise
}
}
174. @genehack › Intro to Swift › TPC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
175. @genehack › Intro to Swift › TPC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
176. @genehack › Intro to Swift › TPC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
177. @genehack › Intro to Swift › TPC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
178. @genehack › Intro to Swift › TPC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
179. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
180. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
181. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
182. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
183. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set (age) {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
184. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set (age) {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
185. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
186. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
willSet {
// runs before property value changes
}
didSet {
// runs after property value changes
}
}
}
187. @genehack › Intro to Swift › TPC 2017
Computed Properties
class Dog {
var age :Int {
willSet {
// runs before property value changes
}
didSet {
// runs after property value changes
}
}
}
197. @genehack › Intro to Swift › TPC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
198. @genehack › Intro to Swift › TPC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
199. @genehack › Intro to Swift › TPC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
200. @genehack › Intro to Swift › TPC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
201. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
}
var conf = OpenSourceConfs.TPC
// conf is type OpenSourceConfs
202. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
}
var conf = OpenSourceConfs.TPC
// conf is type OpenSourceConfs
203. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
}
var conf = OpenSourceConfs.TPC
// conf is type OpenSourceConfs
204. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
}
var conf = OpenSourceConfs.TPC
// conf is type OpenSourceConfs
205. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs :Int {
case TPC = 1
case YAPC
}
var conf = OpenSourceConfs.YAPC
// conf is type OpenSourceConfs
conf.rawValue // 2
206. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs :Int {
case TPC = 1
case YAPC
}
var conf = OpenSourceConfs.YAPC
// conf is type OpenSourceConfs
conf.rawValue // 2
207. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs :Int {
case TPC = 1
case YAPC
}
var conf = OpenSourceConfs.YAPC
// conf is type OpenSourceConfs
conf.rawValue // 2
208. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs :Int {
case TPC = 1
case YAPC
}
var conf = OpenSourceConfs.YAPC
// conf is type OpenSourceConfs
conf.rawValue // 2
209. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
func describe() -> String {
switch self {
case .TPC:
return "hello virginia!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC
conf.describe()
210. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
func describe() -> String {
switch self {
case .TPC:
return "hello virginia!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC
conf.describe()
211. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
func describe() -> String {
switch self {
case .TPC:
return "hello virginia!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC
conf.describe()
212. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC
case YAPC
func describe() -> String {
switch self {
case .TPC:
return "hello virginia!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC
conf.describe()
213. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC(let location):
return "hello (location)!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC("DC")
conf.describe()
214. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC(let location):
return "hello (location)!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC("DC")
conf.describe()
215. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC(let location):
return "hello (location)!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC("DC")
conf.describe()
216. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC(let location):
return "hello (location)!"
case .YAPC:
return "I thought YAPC was dead"
}
}
}
var conf = OpenSourceConfs.TPC("DC")
conf.describe()
217. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC (let location):
return "hello (location)!"
case .YAPC(location)
return "I thought YAPC was dead"
}
}
}
var tpc2017 = OpenSourceConfs.TPC("DC")
var tpc2018 = OpenSourceConfs.TPC("Pittsurgh?")
218. @genehack › Intro to Swift › TPC 2017
Enumerations
enum OpenSourceConfs {
case TPC(String)
case YAPC
func describe() -> String {
switch self {
case .TPC (let location):
return "hello (location)!"
case .YAPC(location)
return "I thought YAPC was dead"
}
}
}
var tpc2017 = OpenSourceConfs.TPC("DC")
var tpc2018 = OpenSourceConfs.TPC("Pittsurgh?")
219. @genehack › Intro to Swift › TPC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
220. @genehack › Intro to Swift › TPC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
221. @genehack › Intro to Swift › TPC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
222. @genehack › Intro to Swift › TPC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
239. @genehack › Intro to Swift › TPC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
240. @genehack › Intro to Swift › TPC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
241. @genehack › Intro to Swift › TPC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
242. @genehack › Intro to Swift › TPC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
243. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
244. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
245. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
246. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
247. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
248. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
249. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
250. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
251. @genehack › Intro to Swift › TPC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
252. @genehack › Intro to Swift › TPC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
253. @genehack › Intro to Swift › TPC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
254. @genehack › Intro to Swift › TPC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
255. @genehack › Intro to Swift › TPC 2017
Defer
func needsMuchSetupAndTearDown () {
// do the setup here, open files, etc.
defer {
// and do the cleanup here,
// right next to set up
}
// other code here.
}
256. @genehack › Intro to Swift › TPC 2017
Defer
func needsMuchSetupAndTearDown () {
// do the setup here, open files, etc.
defer {
// and do the cleanup here,
// right next to set up
}
// other code here.
}