iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

97
© 2014 Hello24 Ltd. hello24.com 10 reasons you'll love Swift Paul Ardeleanu

Transcript of iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Page 1: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

© 2014 Hello24 Ltd. hello24.com

10 reasons you'll love SwiftPaul Ardeleanu

Page 2: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
Page 3: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

here

Page 4: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
Page 5: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 20145

TIOBE Index for Nov 2014

Page 6: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 20146

TIOBE Index for Nov 2014

Page 7: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Why a new language?

Page 8: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 20148

AAPL

Page 9: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 20149

Products

Page 10: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201410

Software

Page 11: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201411

App Stores

Page 12: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201412

Apps

Page 13: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Apple needs developers

Page 14: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

‣ maximise the number of developers

‣ keeping existing developers happy

14

2 ways…

Page 15: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

‣ 30 year old language

‣ drastically different from other languages

‣ not entirely future-proof

15

Objective-C

Page 16: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

How did this happen?

Page 17: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

LLVM & Clang

gcc => llvm-gcc => llvm

Page 18: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
Page 19: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

Objective-C without C

19

Objective-C is to Swift == cat is to cattle

Swift

Page 20: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

10 reasons you'll love Swift

Page 21: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

1. No C

Page 22: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201422

No main( ) function

Page 23: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201423

No [, ] or ;

Page 24: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

2. Modern

Page 25: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 2014

let http404Error = (404, "Not Found")

25

Tuples

let x = 1 let y = 2

let point = (x, y) (.0 1, .1 2)point.0 1

let origin = (x: 200, y: 100) origin.y

(.0 200, .1 100)100

Page 26: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 2014

// Original var fibonacci = 1 var prev = 0

while fibonacci < 100 { var prev_tmp = fibonacci fibonacci += prev prev = prev_tmp println(fibonacci) }

26

Fibonacci Tuples

// tuples var fibonacci = 1 var prev = 0

while fibonacci < 100 { (prev, fibonacci) = (fibonacci, fibonacci + prev) println(fibonacci) }

Page 27: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201427

Nil coalescing operator

var b = a ?? "default value"

Page 28: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

3. Safety

Page 29: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

let theAnswer = 42

29

Constants & variables

var numberBooks = 1

var numberBooks: Int = 1

Page 30: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201430

Safety - no implicit conversion

let theAnswer2 = 42

let trueAnswer = Double(theAnswer) + 0.0001

let aNumber:UInt8 = 42

let anotherNumber:Int64 = Int64(aNumber)

Page 31: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201431

Safety - switch

switch (aNumber % 3, aNumber % 5 ) { case (0, 0): println("fizzbuzz") case (0, _): println("fizz") case (_, 0): println("buzz") default: println(aNumber) }

Page 32: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 06 Slide Hello24 Ltd. (c) 201432

Classes vs. Structuresstruct Point { var x = 0 var y = 0 static var counter = 0 static let origin = Point() var description: String { get { switch (point.x, point.y) { case (let 0, 0): return "origin" case (let x, 0): return "on the y axis - x = \(x)" case (0, let y): return "on the x axis - y = \(y)" default: return "\(point.x)x\(point.y)" } } } func distanceToOrigin() -> Double { return … } }

class Fruit { var color: String var diameter: Int

var area: Double { get { return self.width * self.height } set(newValue) { self.width = sqrt(newValue) self.height = sqrt(newValue) } }

init(color: String, diameter: Int) { self.color = color self.diameter = diameter }

func area() -> Double { return self.width * self.height } }

Page 33: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 06 Slide Hello24 Ltd. (c) 201433

Classes vs. Structures

Classes Structs

passed by reference value

inheritance ✔ ✘

initializers must be defined auto-generated member-wise initialiser

deinitializer ✔ ✘

introspection ✔ ✘

ideal for complex data relatively simple data *

Page 34: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 05 Slide Hello24 Ltd. (c) 201434

Value type (Structures)

var point1 = Point(x: 100, y: 200) var point2 = point1

x: 100 y : 200point1

x: 100 y : 200point2

point1.x = 120

Page 35: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 05 Slide Hello24 Ltd. (c) 201435

Value type (Structures)

var point1 = Point(x: 100, y: 200) var point2 = point1

x: 120 y : 200point1

x: 100 y : 200point2

point1.x = 120

Page 36: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 06 Slide Hello24 Ltd. (c) 201436

Reference type (Classes)

var rectangle1 = Rect() rectangle1.width = 100

width: 100 height: 0rectangle1

rectangle2

var rectangle2 = rectangle1

rectangle2.width = 200

Page 37: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 06 Slide Hello24 Ltd. (c) 201437

Reference type (Classes)

var rectangle1 = Rect() rectangle1.width = 100

width: 200 height: 0rectangle1

rectangle2

var rectangle2 = rectangle1

rectangle2.width = 200

Page 38: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

nil

Page 39: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
Page 40: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

4. Optionals

Page 41: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201441

Optionals

var answer: Int = 42

var theAnswer: Int = nil

var theAnswer: Int? ✔

theAnswer = 42

nil

{Some 42}

❗️

Page 42: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201442

Optionals == Schrödinger’s cat

Can either:

‣ be nil

‣ contain a value

Page 43: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201443

Using an optional

var theAnswer: Int?

if theAnswer != nil { var x = 4 + theAnswer }

❗️

Page 44: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201444

Forced Unwrapping

var theAnswer: Int?

if theAnswer != nil { var x = 4 + theAnswer! }

Page 45: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201445

Optional binding

if let answer = theAnswer { var x = 4 + answer }

if theAnswer != nil { var x = 4 + theAnswer! }

no !

Page 46: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201446

Optionals

var theAnswer: Int?

if theAnswer != nil { var x = 4 + theAnswer! }

theAnswer!

Page 47: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201447

Optionals

var theAnswer = 42

var theAnswer: Int?

var theAnswer: Int!

- always has a value - type can be inferred

- either nil or has a value - must be unwrapped

- nil until first assignment - assumed to always have a value

Page 48: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201448

Nil coalescing operator

equivalent to:

var theAnswer: Int?

var x = theAnswer ?? 12

var x = (theAnswer != nil) ? theAnswer! : 12

Page 49: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

5. Mix & match

Page 50: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201450

Swift project

Page 51: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201451

Bridging

Page 52: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201452

Bridging

Page 53: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201453

Bridging

class STDataObject: NSManagedObject { @NSManaged var uuid: String? @NSManaged var sync_uuid: String? @NSManaged var is_active: NSNumber class func managedObjectContext() -> NSManagedObjectContext? { var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate return appDelegate.managedObjectContext }

Page 54: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

6. Functions as first class citizens

Page 55: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201455

Function structure

func greet(title: String, person: String) -> String { return "Hello \(title) \(person)" }

function name

return type

greet("Mr.", "Paul") “Hello Mr. Paul"

parameter name

parameter type

Page 56: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201456

Nestingfunc greet(var person: String) -> String { func morningGreetings(person: String) -> String { return "Good morning \(person)" } func afternoonGreetings(person: String) -> String { return "Good afternoon \(person)" } let morning = true var greeting: (String) -> String if morning { greeting = morningGreetings } else { greeting = afternoonGreetings } return greeting(person) }

greet("Paul")

Page 57: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201457

Function as return typefunc greetingAt(hour: Int) -> (String) -> String { func morningGreeting(name: String) -> String { return "Good morning \(name)" } func afternoonGreeting(name: String) -> String { return "Good afternoon \(name)" } return hour < 12 ? morningGreeting : afternoonGreeting }

greetingAt(11)("Paul")

greetingAt(14)("Paul")

“Good morning Paul"

“Good afternoon Paul"

Page 58: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Functions are Closures

Page 59: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201459

Blocks in Objective-C

NSString *(^sayHello)(NSString *);

sayHello = ^(NSString *name) { return [NSString stringWithFormat:@"Hello %@", name]; };

Person *theUser = [[Person alloc] initWithName:@"Paul"]; [theUser welcomeUserWithBlock:^(NSString *name) { NSLog(@"Hello %@", name); }];

Page 60: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201460

Blocks in Objective-C

func greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); }

greetWithMessage("Paul", { (name: String) -> String in return "Good morning \(name)" } )

Page 61: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 2014

• global function

• have name & don’t capture environment

• nested function

• have name & capture environment

• closure expressions

• no name & can capture environment

61

Types of Closures

Page 62: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

7. Magic

Page 63: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201463

Closures - Short Syntaxfunc greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); }

greetWithMessage("Paul", { (name: String) -> String in return "Good morning \(name)" } )

greetWithMessage("Paul", { name in return "Good morning \(name)" })

greetWithMessage("Paul", { return "Good morning \($0)" })

Page 64: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 04 Slide Hello24 Ltd. (c) 201464

Trailing closuresfunc greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); }

greetWithMessage("Paul", { name in return "Good morning \(name)" })

greetWithMessage("Paul") { name in return "Good morning \(name)" }

greetWithMessage("Paul") { return "Good morning \($0)" }

Page 65: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

8. Unicode

Page 66: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

8. Emoji

Page 67: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201467

Characters & Strings

let fizz = "🍹" let buzz = "🎉"

var fizzbuzz = fizz + buzz "🍹🎉"

let dog = "🐶"

Page 68: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201468

Character Viewer

Page 69: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 2014

let 🐶 = "dog"

let 🐱 = "cat"

let 💃🐞 = "ladybug"

let ❄️🚶 = "⛄️"

69

Emoji

Page 70: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201470

Page 71: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201471

Page 72: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201472

Unicodelet a = "\u{103}"

let sh = "\u{15F}"

"ă"

"ş"

let cedilla = "\u{327}" "̧"

Extended grapheme cluster

let sh2 = "\u{73}\u{327}"

"\u{15F}" == "\u{73}\u{327}" true

ș = s + cedilla

"ş"

Page 73: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Module 02 Slide Hello24 Ltd. (c) 201473

Unicode

let sh = "\u{15F}" "ş"

let realSh = "\u{219}" "ș"

Page 74: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

9. Playgrounds

Page 75: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
Page 76: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201476

New Playground

Page 77: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201477

Empty Playground

Page 78: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201478

Playground

Page 79: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201479

Playground

Page 80: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201480

Fibonacci

Page 81: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201481

Fibonacci

Page 82: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201482

Fibonacci

Page 83: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201483

Playground - Timeline

Page 84: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

‣ Interactive experience

‣ Immediate feedback

‣ Watch code progression through loops

‣ Easy way to

‣ prototype

‣ test snippets of code

‣ CAREFUL! Executed automatically.

84

Playgrounds

Page 85: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

10. Swift REPL

Page 86: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201486

REPLRead–eval–print loop

$ which swift

/usr/bin/swift

$ swift -version

Swift version 1.1 (swift-600.0.54.20)

Target: x86_64-apple-darwin14.0.0

Page 87: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201487

man swift

Page 88: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201488

swift -- help

Page 89: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

$ swift Welcome to Swift! Type :help for assistance. 1> 1 + 2 $R0: Int = 3 2> "once upon a time" $R1: String = "once upon a time" 3> $R1 + " there were \($R0) bears" $R2: String = "once upon a time there were 3 bears" 4> println($R2) once upon a time there were 3 bears

89

REPL

Page 90: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

‣ iOS 8 released yesterday

‣ Swift is v.1.0 as of Sept 9th

‣ Xcode 6.0.1 released yesterday

‣ Apps written in Swift started being accepted on Sept 9th

90

Resources

Page 91: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

‣ iOS 8 released yesterday

‣ Swift is v.1.0 as of Sept 9th

‣ Xcode 6.0.1 released yesterday

‣ Apps written in Swift started being accepted on Sept 9th

91

Resources

Page 92: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 2014

1.No C

2.Modern

3.Safety

4.Optionals

5.Mix & match

92

10 reasons you’ll love Swift

6.Functions - 1st class citizens

7.Magic

8.Unicode

9.Playgrounds

10.REPL

Page 93: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Objective-C ➾ Swift

Page 94: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

Slide Hello24 Ltd. (c) 201494

Page 95: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

One more thing…

Page 96: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

WatchKit

Page 97: iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

© 2014 Hello24 Ltd. hello24.com

Thank you!

Paul Ardeleanu

[email protected] @pardel