Date post: | 11-Feb-2017 |
Category: |
Software |
Upload: | pablo-villar |
View: | 125 times |
Download: | 0 times |
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
OptionalsAn optional is a type
OptionalsAn optional is a type
An optional is actually an enum, so that:
OptionalsAn optional is a type
An optional is actually an enum, so that:
OptionalsAn optional is a type
An optional is actually an enum, so that:
OptionalsAn optional is a type
An optional is actually an enum, so that:
OptionalsAn optional is a type
An optional is actually an enum, so that:
OptionalsString? “A String that can be nil”
OptionalsString? “A String that can be nil”
OptionalsString? “A String that can be nil”
An Optional that can be a String
OptionalsString? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
OptionalsString? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
Stringnil
OptionalsString? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
Stringnil stringThatCanBeNil
// not a String, but a String?
OptionalsString? “A String that can be nil”
An Optional that can be a String
…or can eventually be nil
String?
Stringnil stringThatCanBeNil
// not a String, but a String?
stringThatCanBeNil!// The ! operator "unwraps" the optional, // you get String then
OptionalsBut… warning!
If you try to unwrap an optional and it
happens to be nil, the app will crash!
OptionalsBut… warning!
If you try to unwrap an optional and it
happens to be nil, the app will crash!
OptionalsSo, should we use the unwrapping (!) operator…?
OptionalsSo, should we use the unwrapping (!) operator…?
Well, yes (in some cases)… Because sometimes we do want the app to crash!
OptionalsSo, should we use the unwrapping (!) operator…?
Well, yes (in some cases)… Because sometimes we do want the app to crash!
Example:
OptionalsSo, should we use the unwrapping (!) operator…?
Well, yes (in some cases)… Because sometimes we do want the app to crash!
Example:
OptionalsSo, 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
OptionalsSo, 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
OptionalsSo, 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
OptionalsObj-C
OptionalsObj-C
Swift
OptionalsThe ?? operator
OptionalsThe ?? operator
let a: Int?let b: Intlet result: Int
OptionalsThe ?? operator
if let value = a { result = value } else { result = b }
let a: Int?let b: Intlet result: Int
OptionalsThe ?? operator
if let value = a { result = value } else { result = b }
result = a ? a! : b
let a: Int?let b: Intlet result: Int
OptionalsThe ?? operator
result = a ?? b
if let value = a { result = value } else { result = b }
result = a ? a! : b
let a: Int?let b: Intlet result: Int
TipsA good place to set a delegate
How to declare constants in your class
TipsInteresting piece of code
TipsInteresting piece of code
Defining a computed property
TipsInteresting piece of code
Define constants
Defining a computed property
TipsInteresting piece of code
Define constants
Using as?
Defining a computed property
TipsInteresting 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 propertyhttp://stackoverflow.com/questions/24215578/when-should-i-access-properties-with-self-in-swift
Fun with arrays!
sort
filter
map
reduce
Fun with arrays!