2. Switch
switch is much more powerful in Swift than in Obj-C
You can replace lots of if statements by switch statements
3. Even if you’re able to do something with a single if statement,
sometimes it has much more sense to think of a switch
instead, for example:
Switch
4. Advantage
Code intention is clearer, it tells you that you have (or might
have) more options to switch over.
With a single if, you don’t realize that.
Switch
5. When you combine the powerful of switch with enums, you
are working on the base of the Pattern Matching concept.
case .Delayed(let minutes)
println("delayed by (minutes) minutes”
Switch & Enum
7. A very interesting use case of Pattern Matching in our world is
for completion functions…
Another interesting use case
Completion is a type.
.Success with Array<T>
.Failure with NSError
… or …
Switch & Enum
9. It takes a function as argument, whose argument is a
Completion type.
The function is executed here, passing a .Success case
with an array of Contributions.
Switch & Enum
10. It takes a function as argument, whose argument is a
Completion type.
The function is executed here, passing a .Success case
with an array of Contributions.
Computed property
Switch & Enum
26. Optionals
String? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
String
nil
27. Optionals
String? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
String
nil
stringThatCanBeNil
// not a String, but a String?
28. Optionals
String? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
String
nil
stringThatCanBeNil
// not a String, but a String?
stringThatCanBeNil!
// The ! operator "unwraps" the optional,
// you get String then
32. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
33. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
34. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
35. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
Will return TransactionCell if possible,
nil otherwise
36. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
Will return TransactionCell if possible,
nil otherwise
37. Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
Will return TransactionCell if possible,
nil otherwise
43. Optionals
The ?? operator
if let value = a {
result = value
} else {
result = b
}
let a: Int?
let b: Int
let result: Int
44. Optionals
The ?? operator
if let value = a {
result = value
} else {
result = b
}
result = a ? a! : b
let a: Int?
let b: Int
let result: Int
45. Optionals
The ?? operator
result = a ?? b
if let value = a {
result = value
} else {
result = b
}
result = a ? a! : b
let a: Int?
let b: Int
let result: Int
46. Tips
A good place to set a delegate
How to declare constants in your class