forked from udacity/ios-nd-swift-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contents.swift
196 lines (106 loc) · 3.06 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//: # All about Optionals!
import UIKit
//: ### Example 1: Nil is disallowed in most Swift types
// Ints can't store nil values
var x: Int
//x = nil
// Object references can't store nil values either
var c: AnyObject
c = UIColor.red
//c = nil
//: ### Cool! Except, sometimes we need nil values.
//: 1. A method that cannot return a value
var y: Int?
var s1: String
var s2: String
s1 = "123"
s2 = "ABC"
y = Int(s1)
y = Int(s2)
//: 2. Properties that cannot be initialized when an object is constructed
class ViewController: UIViewController {
var button: UIButton!
}
//: ### Declaring optionals with Question Marks
// Example 1
var z: Int?
var string: String
string = "123"
z = Int(string)
//z * 2
// Example 2
class AnotherViewController: UIViewController {
//var anotherButton: UIButton
}
//: ### Unwrapping Optionals with if let
// Example 1: A scenario that could return an optional with a nil value
var zee: Int?
let strings = ["ABC","123"]
let randomIndex = Int(arc4random() % 2)
let anotherString = strings[randomIndex]
zee = Int(anotherString)
if let intValue = zee {
intValue * 2
} else {
"No value"
}
//Example 2: Another example with UIImageView
var imageView = UIImageView()
imageView.image = UIImage(named:"puppy_in_box")
if let image = imageView.image {
let size = image.size
} else {
print("This image hasn't been set.")
}
imageView.image
//: ### Optional Chaining
// Example 1
var anotherImageView = UIImageView()
anotherImageView.image = UIImage(named:"puppy_in_box")
var size = anotherImageView.image?.size
type(of: size)
if let imageSize = anotherImageView.image?.size {
print("Here's the image size: \(imageSize)")
} else {
print("This image hasn't been set.")
}
imageView.image
// Example 2
var animal = Animal(name: "Lenny", species: "lemur", tailLength: 12)
animal = Animal(name: "Gilbert", species: "Gorilla", tailLength: nil )
if let tailLength = animal.tail?.length {
print("\(animal.name)'s tail is \(tailLength) long")
} else {
print("\(animal.name) doesn't have a tail.")
}
//: ### Implicitly Unwrapped Optionals
// Example 1
let w = Int("123")!
w * 2
// Example 2
class BetterViewController: UIViewController {
var myButton: UIButton!
}
//: ### Optionals can be passed as parameters to functions
// Example: Picking up groceries in an optional car
func pickUpGroceries(_ car:Car?) {
if let car = car {
print("We'll pick up the groceries in the \(car.model)")
} else {
print("Let's walk to the store")
}
}
var someCar = Car(make: "Toyota", model: "Corolla")
pickUpGroceries(someCar)
pickUpGroceries(nil)
// Example: Hosting a guest in an optional extra room
func host(_ guest: String, extraRoom: Room?) {
if let extraRoom = extraRoom {
print("Come stay with us, \(guest), you can sleep in the \(extraRoom.name).")
} else {
print("Come stay with us, \(guest), you can sleep on the couch.")
}
}
var someRoom = Room(name: "guest room", occupied: false)
host("Grandma", extraRoom: someRoom)
host("Ryan", extraRoom: nil)