+ All Categories
Home > Software > Little lessons learned from Swift

Little lessons learned from Swift

Date post: 11-Feb-2017
Category:
Upload: pablo-villar
View: 125 times
Download: 0 times
Share this document with a friend
56
Little lessons learned from Swift Very first steps after transitioning from Objective-C
Transcript
Page 1: Little lessons learned from Swift

Little lessons learned from Swift

Very first steps after transitioning from Objective-C

Page 2: Little lessons learned from Swift

Switch

switch is much more powerful in Swift than in Obj-C

You can replace lots of if statements by switch statements

Page 3: Little lessons learned from Swift

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

Page 4: Little lessons learned from Swift

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

Page 5: Little lessons learned from Swift

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

Page 6: Little lessons learned from Swift

Table view sections

Interesting use case

“raw” type

= 0= 1= 2

// implicit // raw // values

toRaw

fromRaw

Switch & Enum

Page 7: Little lessons learned from Swift

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

Page 8: Little lessons learned from Swift

Switch & Enum

*switches in Swift don’t fall through automatically

Page 9: Little lessons learned from Swift

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

Page 10: Little lessons learned from Swift

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

Page 11: Little lessons learned from Swift

Computed Properties

// process whatever you need // in order to return the array

Page 12: Little lessons learned from Swift

Computed Properties

Page 13: Little lessons learned from Swift

Computed Properties

Page 14: Little lessons learned from Swift

Computed Properties

Page 15: Little lessons learned from Swift

Optionals

Page 16: Little lessons learned from Swift

OptionalsAn optional is a type

Page 17: Little lessons learned from Swift

OptionalsAn optional is a type

An optional is actually an enum, so that:

Page 18: Little lessons learned from Swift

OptionalsAn optional is a type

An optional is actually an enum, so that:

Page 19: Little lessons learned from Swift

OptionalsAn optional is a type

An optional is actually an enum, so that:

Page 20: Little lessons learned from Swift

OptionalsAn optional is a type

An optional is actually an enum, so that:

Page 21: Little lessons learned from Swift

OptionalsAn optional is a type

An optional is actually an enum, so that:

Page 22: Little lessons learned from Swift

OptionalsString? “A String that can be nil”

Page 23: Little lessons learned from Swift

OptionalsString? “A String that can be nil”

Page 24: Little lessons learned from Swift

OptionalsString? “A String that can be nil”

An Optional that can be a String

Page 25: Little lessons learned from Swift

OptionalsString? “A String that can be nil”

An Optional that can be a String

…or can eventually be nil

Page 26: Little lessons learned from Swift

OptionalsString? “A String that can be nil”

An Optional that can be a String

…or can eventually be nil

String?

Stringnil

Page 27: Little lessons learned from Swift

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?

Page 28: Little lessons learned from Swift

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

Page 29: Little lessons learned from Swift

OptionalsBut… warning!

If you try to unwrap an optional and it

happens to be nil, the app will crash!

Page 30: Little lessons learned from Swift

OptionalsBut… warning!

If you try to unwrap an optional and it

happens to be nil, the app will crash!

Page 31: Little lessons learned from Swift

OptionalsSo, should we use the unwrapping (!) operator…?

Page 32: Little lessons learned from Swift

OptionalsSo, should we use the unwrapping (!) operator…?

Well, yes (in some cases)… Because sometimes we do want the app to crash!

Page 33: Little lessons learned from Swift

OptionalsSo, should we use the unwrapping (!) operator…?

Well, yes (in some cases)… Because sometimes we do want the app to crash!

Example:

Page 34: Little lessons learned from Swift

OptionalsSo, should we use the unwrapping (!) operator…?

Well, yes (in some cases)… Because sometimes we do want the app to crash!

Example:

Page 35: Little lessons learned from Swift

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

Page 36: Little lessons learned from Swift

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

Page 37: Little lessons learned from Swift

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

Page 38: Little lessons learned from Swift

Optionals

amount is String?

value is String

// value doesn’t need to be unwrapped

"if let" unwraps variables automatically

Page 39: Little lessons learned from Swift

OptionalsObj-C

Page 40: Little lessons learned from Swift

OptionalsObj-C

Swift

Page 41: Little lessons learned from Swift

OptionalsThe ?? operator

Page 42: Little lessons learned from Swift

OptionalsThe ?? operator

let a: Int?let b: Intlet result: Int

Page 43: Little lessons learned from Swift

OptionalsThe ?? operator

if let value = a { result = value } else { result = b }

let a: Int?let b: Intlet result: Int

Page 44: Little lessons learned from Swift

OptionalsThe ?? operator

if let value = a { result = value } else { result = b }

result = a ? a! : b

let a: Int?let b: Intlet result: Int

Page 45: Little lessons learned from Swift

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

Page 46: Little lessons learned from Swift

TipsA good place to set a delegate

How to declare constants in your class

Page 47: Little lessons learned from Swift

TipsInteresting piece of code

Page 48: Little lessons learned from Swift

TipsInteresting piece of code

Defining a computed property

Page 49: Little lessons learned from Swift

TipsInteresting piece of code

Define constants

Defining a computed property

Page 50: Little lessons learned from Swift

TipsInteresting piece of code

Define constants

Using as?

Defining a computed property

Page 51: Little lessons learned from Swift

TipsInteresting piece of code

Define constants

Using as?

Defining a computed property Using ?? operator

Page 52: Little lessons learned from Swift

Syntax Sugar

// initialized nil by default

// initialized nil by default

Page 53: Little lessons learned from Swift

self.property vs property

<< the debate >>

Page 54: Little lessons learned from Swift

self.property vs propertyhttp://stackoverflow.com/questions/24215578/when-should-i-access-properties-with-self-in-swift

Page 55: Little lessons learned from Swift

Fun with arrays!

sort

filter

map

reduce

Page 56: Little lessons learned from Swift

Fun with arrays!


Recommended