Little lessons
learned from Swift
Very first steps after
transitioning from Objective-C
Switch
switch is much more powerful in Swift than in Obj-C
You can replace lots of if statements by switch statements
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
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
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
Table view sections
Interesting use case
“raw” type
= 0
= 1
= 2
// implicit
// raw
// values
toRaw
fromRaw
Switch & Enum
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
Switch & Enum
*switches in Swift don’t fall through automatically
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
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
Computed Properties
// process whatever you need
// in order to return the array
Computed Properties
Computed Properties
Computed Properties
Optionals
Optionals
An optional is a type
Optionals
An optional is a type
An optional is actually an enum, so that:
Optionals
An optional is a type
An optional is actually an enum, so that:
Optionals
An optional is a type
An optional is actually an enum, so that:
Optionals
An optional is a type
An optional is actually an enum, so that:
Optionals
An optional is a type
An optional is actually an enum, so that:
Optionals
String? “A String that can be nil”
Optionals
String? “A String that can be nil”
Optionals
String? “A String that can be nil”
An Optional that can be a String
Optionals
String? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
Optionals
String? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
String
nil
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?
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
Optionals
But… warning!
If you try to unwrap an optional and it
happens to be nil, the app will crash!
Optionals
But… warning!
If you try to unwrap an optional and it
happens to be nil, the app will crash!
Optionals
So, should we use the unwrapping (!) operator…?
Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
Optionals
So, should we use the unwrapping (!) operator…?
Well, yes (in some cases)…
Because sometimes we do want the app to crash!
Example:
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
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
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
Optionals
amount is String?
value is String
// value doesn’t need to be unwrapped
"if let" unwraps variables automatically
Optionals
Obj-C
Optionals
Obj-C
Swift
Optionals
The ?? operator
Optionals
The ?? operator
let a: Int?
let b: Int
let result: Int
Optionals
The ?? operator
if let value = a {
result = value
} else {
result = b
}
let a: Int?
let b: Int
let result: Int
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
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
Tips
A good place to set a delegate
How to declare constants in your class
Tips
Interesting piece of code
Tips
Interesting piece of code
Defining a computed property
Tips
Interesting piece of code
Define constants
Defining a computed property
Tips
Interesting piece of code
Define constants
Using as?
Defining a computed property
Tips
Interesting piece of code
Define constants
Using as?
Defining a computed property
Using ?? operator
Syntax Sugar
// initialized nil by default
// initialized nil by default
self.property vs property
<< the debate >>
self.property vs property
http://stackoverflow.com/questions/24215578/when-should-i-access-properties-with-self-in-swift
Fun with arrays!
sort
filter
map
reduce
Fun with arrays!

Little lessons learned from Swift