Developing Swift - Moving towards the future

51
Developing Swift Moving towards the future

Transcript of Developing Swift - Moving towards the future

Page 1: Developing Swift - Moving towards the future

Developing SwiftMoving towards the future

Page 2: Developing Swift - Moving towards the future

About Inaka• Started in 2010.

• Acquired by Erlang Solutions in 2014.

• We build end-to-end applications with Erlang, Ruby, Web, iOS and Android components.

• We develop highly concurrent application servers such as Whisper and TigerText.

• We're active contributors to the open-source community.

Page 3: Developing Swift - Moving towards the future

About Me• Pablo Villar.

• iOS Developer.

• Have been working at Inaka for last 2 years.

• Have been working as iOS dev for last 4 years.

• Clean code lover!

Page 4: Developing Swift - Moving towards the future

What’s Swift?

Why Swift?

How to Swift?

What’s behind Swift?

What’s Swift for?

Page 5: Developing Swift - Moving towards the future

A little bit of history…

Early 1980's Objective-C

2007 iPhone is born

Late 1980's NeXT license AppKit, FoundationKit

1997 Apple purchases NeXT

2010 iPad is born2015 Watch is born

Page 6: Developing Swift - Moving towards the future

A little bit of history…

Early 1980's Objective-C

2007 iPhone is born

Late 1980's NeXT license AppKit, FoundationKit

1997 Apple purchases NeXT

2010 iPad is born2015 Watch is born

2014

Swift

Page 7: Developing Swift - Moving towards the future

2007~2015 (last 8 years)

… ?

IPHONE

IPAD

WATCH

IPOD TOUCH

Page 8: Developing Swift - Moving towards the future

OBJ-C SWIFT

1980 - Nowadays 2014 - Nowadaysto be deprecated… (?) to keep growing…

Page 9: Developing Swift - Moving towards the future

A very little bit of history of Swift…

June 2014 Swift 1.0

April 2015 Swift 1.2

October 2015 Swift 2.0

Today

No backwards compatibility

OPEN SOURCE!

Page 10: Developing Swift - Moving towards the future

BE ADAPTATIVE!

Page 11: Developing Swift - Moving towards the future

BE PATIENT!

Y U NO WORK?!?!?!

Page 12: Developing Swift - Moving towards the future

BE TOLERANT!

Y U NO CLEAR?!?!?!

Page 13: Developing Swift - Moving towards the future

BE ENTHUSIASTIC!

Page 14: Developing Swift - Moving towards the future

BE ENTHUSIASTIC!

Page 15: Developing Swift - Moving towards the future

BE ENTHUSIASTIC!

Page 16: Developing Swift - Moving towards the future

BE CHALLENGED!

Last… Not least.

Page 17: Developing Swift - Moving towards the future

Now… What you’ve been waiting for…

Page 18: Developing Swift - Moving towards the future

Now… What you’ve been waiting for…

code!

Page 19: Developing Swift - Moving towards the future

Now… What you’ve been waiting for…

code!

wasn’t it…?

Page 20: Developing Swift - Moving towards the future

Now… What you’ve been waiting for…

code!

SAFETY

Optionals

wasn’t it…?

Page 21: Developing Swift - Moving towards the future

Now… What you’ve been waiting for…

code!

SAFETY

Optionals

String?

Stringnil

wasn’t it…?

Page 22: Developing Swift - Moving towards the future

Why Optionals?

Things can be nullable

You need a mechanism to properly deal with that

Swift is statically typed

+

Page 23: Developing Swift - Moving towards the future

Why Optionals?

Things can be nullable

You need a mechanism to properly deal with that

Swift is statically typed

+

String “A regular String, it can’t be nil”

Page 24: Developing Swift - Moving towards the future

Why Optionals?

Things can be nullable

You need a mechanism to properly deal with that

Swift is statically typed

+

String “A regular String, it can’t be nil”

String? “A String that can be nil”

Optional Type

Page 25: Developing Swift - Moving towards the future

String? “A String that can be nil”

Page 26: Developing Swift - Moving towards the future

String? “A String that can be nil”

Page 27: Developing Swift - Moving towards the future

String? “A

An Optional that can be a String

…or can eventually be nil

String?

Stringnil

Page 28: Developing Swift - Moving towards the future

String? “A

An Optional that can be a String

…or can eventually be nil

String?

Stringnil Behind the scenes…

enum Optional<T> { case None // nil case Some(T) // a T }

Page 29: Developing Swift - Moving towards the future

String? “A

An Optional that can be a String

…or can eventually be nil

String?

Stringnil Behind the scenes…

enum Optional<T> { case None // nil case Some(T) // a T }

Optional<String>

Page 30: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

Page 31: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

var age: Int?

Page 32: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

// We prompt the user to enter his/her age.

var age: Int?

Page 33: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

func greetingForPersonWithAge(age: Int) -> String {

// We prompt the user to enter his/her age.

var age: Int?

switch age { case 0...2: return "Hello baby!" case 3...5: return "How are you doing, toddler?” case 6...12: return "What's up, kid?" case 13...18: return "Yo, what's up?" default: return "Hello, mister!" } }

Page 34: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

func greetingForPersonWithAge(age: Int) -> String {

// We prompt the user to enter his/her age.

var age: Int?

let greeting = greetingForPersonWithAge(age)

println(greeting)

switch age { case 0...2: return "Hello baby!" case 3...5: return "How are you doing, toddler?” case 6...12: return "What's up, kid?" case 13...18: return "Yo, what's up?" default: return "Hello, mister!" } }

Page 35: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

func

// We prompt the user to enter his/her age.

var

let greeting = greetingForPersonWithAge(age)

println(

} }

Page 36: Developing Swift - Moving towards the future

Int?

Intnil

A little, yet valuable example…

func greetingForPersonWithAge(age: Int) -> String {

// We prompt the user to enter his/her age.

var age: Int?

// Int? and Int are not compatible, // You have to deal with it

let greeting = greetingForPersonWithAge(age)

println(greeting)

} }

Page 37: Developing Swift - Moving towards the future

But, how to…

?

Page 38: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Page 39: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Init to nil by default

Page 40: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Init to nil by default

"Unwrap" the optional

Page 41: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Init to nil by default

"Unwrap" the optional

Use the unwrapped one, which is NOT an optional

but an Int

Page 42: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Init to nil by default

"Unwrap" the optional

Use the unwrapped one, which is NOT an optional

but an Int

Define a separated behavior to deal with nil values

Page 43: Developing Swift - Moving towards the future

But, how to…

?

var age: Int? = nil

// We prompt the user to enter his/her age.

if let knownAge = age { let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else { println("Hello") // No age. }

Init to nil by default

"Unwrap" the optional

Use the unwrapped one, which is NOT an optional

but an Int

Define a separated behavior to deal with nil values

SAFETY

Page 44: Developing Swift - Moving towards the future

A little, yet valuable example…

func greetingForPersonWithAge(age: Int) -> String {

// We prompt the user to enter his/her age.

var age: Int?

if let knownAge = age {

let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else {

println("Hello") // Or show alert. Or whatever.

}

switch age { case 0...2: return "Hello baby!" case 3...5: return "How are you doing, toddler?” case 6...12: return "What's up, kid?" case 13...18: return "Yo, what's up?" default: return "Hello, mister!" } }

(Let’s get ALL the juice from it!)

Page 45: Developing Swift - Moving towards the future

A little, yet valuable example…

func greetingForPersonWithAge(age: Int) -> String {

// We prompt the user to enter his/her age.

var age: Int?

if let knownAge = age {

let greeting = greetingForPersonWithAge(knownAge) println(greeting) } else {

println("Hello") // Or show alert. Or whatever.

}

} }

(Let’s get ALL the juice from it!)

From this point on, we don’t have to worry about nullability ever again!

Page 46: Developing Swift - Moving towards the future

In conclusion…

Page 47: Developing Swift - Moving towards the future

In conclusion…

• Swift changes the way we use to think when it comes to develop mobile apps.

Page 48: Developing Swift - Moving towards the future

In conclusion…

• Swift changes the way we use to think when it comes to develop mobile apps.

• The language has just been born and it’s growing really fast.

Page 49: Developing Swift - Moving towards the future

In conclusion…

• Swift changes the way we use to think when it comes to develop mobile apps.

• We’re still in a phase in which there are many bugs and downsides.

• The language has just been born and it’s growing really fast.

Page 50: Developing Swift - Moving towards the future

In conclusion…

• Swift changes the way we use to think when it comes to develop mobile apps.

• We’re still in a phase in which there are many bugs and downsides.

• The language has just been born and it’s growing really fast.

• But still, promises are big! Swift is the future.

Page 51: Developing Swift - Moving towards the future

For more info… Check these out!

https://developer.apple.com/videos/wwdc/2014/

https://developer.apple.com/videos/wwdc/2015/

https://developer.apple.com/swift/resources/

http://www.objc.io/

Cool blogs to learn from…

http://nshipster.com/

Inaka’s webpage:http://inaka.net/

http://inaka.net/blog