As presented at KCDC 2017:
Swift was originally released in 2014, and Open Sourced by Apple in late 2015. The Open Source release generated an explosion of community interest and support, resulting in ports to other platforms and significant language changes. Swift version 3, which reflects the results of much of this work, was released in September of 2016, bringing with it some significant refinements to the core language and a new package manager.
Swift is a multi-paradigm language, supporting imperative, functional, and object-oriented programming styles. The language is strongly typed but has extensive support for type inference and substantial tooling available in XCode to identify and in some cases automatically fix common programming errors. Swift uses a memory management strategy called automatic reference counting (ARC), freeing programmers from the tedium of manually managing memory allocation. This combination of strong typing, maximal type inference, automatic reference counting (ARC), and excellent tooling results in an experience that can be described as "the Macintosh of programming languages".
This talk will present some of the history of the development of Swift with emphasis on how the Open Source release of the language kick-started activity, review the basic syntax of Swift (with comparisons to similar languages that attendees may be more familiar with), and describe what tools are available to help learn the language, including XCode, the Swift REPL available from XCode, and the new Swift Playgrounds for iPad that debuted with Swift 3 and iOS10. After attending this talk, an attendee with no previous Swift experience will understand exactly why they should be excited about this relatively new programming language and be up to date on exactly what they need to do to dive into Swift coding for themselves.
6. @genehack › Intro to Swift › KCDC 2017
Hi I'm John
aka @genehack
• VP Tech, Infinity Interactive
• Perl tribe
• Polyglot coder
• Just this guy, you know?
45. @genehack › Intro to Swift › KCDC 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_
*/
48. @genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
49. @genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
50. @genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
51. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
55. @genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
56. @genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
57. @genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
58. @genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
59. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
60. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
61. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
62. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown -= 1
}
63. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
64. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
65. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
66. @genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp += 1
} while countUp < 5
67. @genehack › Intro to Swift › KCDC 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 › KCDC 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.")
}
69. @genehack › Intro to Swift › KCDC 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 › KCDC 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!
71. @genehack › Intro to Swift › KCDC 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.")
}
72. @genehack › Intro to Swift › KCDC 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.")
}
73. @genehack › Intro to Swift › KCDC 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.")
}
74. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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(" ¯_(ツ)_/¯ ")
}
77. @genehack › Intro to Swift › KCDC 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(" ¯_(ツ)_/¯ ")
}
78. @genehack › Intro to Swift › KCDC 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)")
}
79. @genehack › Intro to Swift › KCDC 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)")
}
81. @genehack › Intro to Swift › KCDC 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")
}
82. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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
87. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
91. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
94. @genehack › Intro to Swift › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
95. @genehack › Intro to Swift › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
96. @genehack › Intro to Swift › KCDC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
97. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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
}
100. @genehack › Intro to Swift › KCDC 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 › KCDC 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
}
103. @genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
104. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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"]
109. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
114. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
115. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
116. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
117. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
118. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
119. @genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates: [String:Any] = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type [String:Any]
120. @genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
121. @genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
122. @genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
123. @genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
149. @genehack › Intro to Swift › KCDC 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]
150. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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
sorted(by:)!
153. @genehack › Intro to Swift › KCDC 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]
154. @genehack › Intro to Swift › KCDC 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 › KCDC 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]
156. @genehack › Intro to Swift › KCDC 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 › KCDC 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]
158. @genehack › Intro to Swift › KCDC 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 › KCDC 2017
Closures
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
var sorted = numbers.sorted { $0 > $1 }
162. @genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
163. @genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
164. @genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
Class ‘Dog’ has no initializers
165. @genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
166. @genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
167. @genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
168. @genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
169. @genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
170. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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
}
}
}
186. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
202. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
203. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
204. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
205. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
206. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
207. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
208. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
209. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
210. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
211. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
212. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
213. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
214. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
215. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
216. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
217. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC (let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var kcdc2017 = OpenSourceConfs.KCDC("KC")
var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
218. @genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC (let location):
return "Hello (location)!"
case .OpenWest
return "Hello Salt Lake City!"
}
}
}
var kcdc2017 = OpenSourceConfs.KCDC("KC")
var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
219. @genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
220. @genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
221. @genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
222. @genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
239. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
244. @genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
245. @genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
246. @genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
247. @genehack › Intro to Swift › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 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 › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
253. @genehack › Intro to Swift › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
254. @genehack › Intro to Swift › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
255. @genehack › Intro to Swift › KCDC 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 › KCDC 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.
}