Swift•is•m (swɪftɪzəm) adj. circa 2015
A Swift idiom or pattern showing the safer,
compact, and expressive nature of Swift,
usually not achievable in Objective-C
Slide 9
Slide 9 text
Optionals
Slide 10
Slide 10 text
Optionals
Not love at first sight
Slide 11
Slide 11 text
June 2nd: excited
Slide 12
Slide 12 text
June 3rd : fall in ❤️ with Swift syntax
[ ] ;
@
& *
Slide 13
Slide 13 text
June 3rd : fall in ❤️ with Swift syntax
[ ] ;
@
& *
For Sale
Slide 14
Slide 14 text
June 4th : Write actual code
UILabel? doesn’t have a text property
fatal error: unexpectedly found nil while unwrapping an Optional value
Value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?
Slide 15
Slide 15 text
Solution #1
Hecticly add more ? and!
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
Quantum mechanics
Slide 18
Slide 18 text
Erwin Schrödinger
Nobel Prize for Physics (1933)
Slide 19
Slide 19 text
Schrödinger Cat
Slide 20
Slide 20 text
Schrödinger Cat
Slide 21
Slide 21 text
Schrödinger Cat
Dead
AND
Alive
Slide 22
Slide 22 text
Optionals
nil
AND
non-nil
Slide 23
Slide 23 text
Optionals
var a:String?
var b:String
Slide 24
Slide 24 text
Optionals
var cat:Cat?
var cat2:Cat
Slide 25
Slide 25 text
Optionals
var text = cat.name
Slide 26
Slide 26 text
If let statement
if let realCat = cat {
// cat is not nil
println("Hurray, \(realCat.name) is alive !")
} else {
// cat is nil
println("Sorry, your cat is dead…")
}
Slide 27
Slide 27 text
nil coalescing operator
var text = cat?.name ?? "RIP"
Slide 28
Slide 28 text
var text = cat?.name
Slide 29
Slide 29 text
Optionals
var a:String?
• Model a null state
• Tell the compiler
« this could be nil »
• Anything else has a value
Slide 30
Slide 30 text
Making your code safer
• Eliminates whole class of bugs
• nil-related crashes
• uninitialized variables
• incomplete object initialization
• Eliminates useless nil-checks
• Forces necessary nil-checks
Slide 31
Slide 31 text
One more thing…
Slide 32
Slide 32 text
Implicitly Unwrapped Optional
a.k.a, exclamation mark
var a:String!
Slide 33
Slide 33 text
-Dimitri
« Trust me, in theory this can
never be nil »
Slide 34
Slide 34 text
« In theory, there is no difference
between theory and practice.
Slide 35
Slide 35 text
« In theory, there is no difference
between theory and practice.
In practice, there is »
Slide 36
Slide 36 text
90%
Swift crashes
misuse of exclamation mark
Slide 37
Slide 37 text
var text = cat!.name
assert(cat != nil, "Cat cannot be nil")
Slide 38
Slide 38 text
!
? if let
New Best Friends
Slide 39
Slide 39 text
Swift Enums
Slide 40
Slide 40 text
One Table View, 4 States
Loading Items
Book 1
Book 2
Book 3
Book 4
Book 5
Book 6
Book 7
Error
Error Occurred
Unable to connect to host
api.abc.com/v1/example
Empty
No Item yet
Start by adding your first item
Add Item
NSError [Book]
Slide 41
Slide 41 text
Swift enums
enum State {
case Loading
case Error
case Empty
case Items
}
var state:State = .Loading
var data:[Book]?
var error:NSError?
Slide 42
Slide 42 text
Swift enums
enum State {
case Loading
case Error(NSError)
case Empty
case Items([Book])
}
var state:State = .Loading
Enum
Associated Values
Slide 43
Slide 43 text
Associated Values
switch state {
case .Loading:
println("Loading")
case .Error(let error):
println("Error: \(error.localizedDescription)")
case .Empty:
println("Empty")
case .Items(let items):
println("They are \(items.count) items in the array")
}
Slide 44
Slide 44 text
Enum Associated Values
• Associated Values help reduce state
• Protects access to data
• Enforced by compiler
Slide 45
Slide 45 text
Extra coolness
enum State {
case Loading
case Error(NSError)
case Empty
case Items([Book])
}
Slide 46
Slide 46 text
Extra coolness
enum State {
case Loading
case Error(NSError)
case Empty
case Items([T])
}
Generics !
Slide 47
Slide 47 text
Extra coolness
enum Result {
case Error(NSError)
case Result(T)
}
Generic
Result Type!