Transcript
Page 1: To swiftly go where no OS has gone before

Copyleft 2014 me

Swift & iOS

Paul Ardeleanu

[email protected] @pardel

Page 2: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 2

Page 3: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 3

Page 4: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 3

An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…

Page 5: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 3

An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…

The barman looks at them & says…

Page 6: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 3

An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…

The barman looks at them & says…

Gee, it must be in town

Page 7: To swiftly go where no OS has gone before

Copyleft 2014 me

Swift & iOS

Paul Ardeleanu

[email protected] @pardel

Page 8: To swiftly go where no OS has gone before

To swiftly go where no other language has

gone before

Page 9: To swiftly go where no OS has gone before

Good artists copy, Real artists steal

Page 10: To swiftly go where no OS has gone before

Everything is a remix

https://www.flickr.com/photos/mugley/800999028/

Page 11: To swiftly go where no OS has gone before

The talk about that new programming language

everyone talks about but nobody really uses…

Page 12: To swiftly go where no OS has gone before

Yet !

Page 13: To swiftly go where no OS has gone before

Copyleft 2014 me

Swift & iOS

Paul Ardeleanu

[email protected] @pardel

Page 14: To swiftly go where no OS has gone before

Everything is a remix

https://www.flickr.com/photos/mugley/800999028/

Page 15: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 12

Page 16: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 12

Page 17: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 13

Page 18: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 13

#freeJonyIve

Page 19: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 14

Page 20: To swiftly go where no OS has gone before

It’s all about the users

https://www.flickr.com/photos/wyogirl13/6455191187

Page 21: To swiftly go where no OS has gone before

It’s all about the users.

https://www.flickr.com/photos/wyogirl13/6455191187

Page 22: To swiftly go where no OS has gone before
Page 23: To swiftly go where no OS has gone before
Page 24: To swiftly go where no OS has gone before
Page 25: To swiftly go where no OS has gone before
Page 26: To swiftly go where no OS has gone before
Page 27: To swiftly go where no OS has gone before
Page 28: To swiftly go where no OS has gone before

It’s all about the users.

https://www.flickr.com/photos/wyogirl13/6455191187

Page 29: To swiftly go where no OS has gone before
Page 30: To swiftly go where no OS has gone before

Why?

Page 31: To swiftly go where no OS has gone before

Why?What?

Page 32: To swiftly go where no OS has gone before

Why?What?When?

Page 33: To swiftly go where no OS has gone before

Why?What?When?How?

Page 34: To swiftly go where no OS has gone before

Why?

Page 35: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 24

Page 36: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 25

AAPL

Page 37: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 26

Products

Page 38: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 27

Software

Page 39: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 28

App Stores

Page 40: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 29

Page 41: To swiftly go where no OS has gone before

Apple needs developers

Page 42: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide

‣ maximise the number of developers

‣ keeping existing developers happy

31

2 more things…

Page 43: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide

‣ 30 year old language

‣ drastically different from other languages

‣ not entirely future-proof

32

Objective-C

Page 44: To swiftly go where no OS has gone before

What?

Page 45: To swiftly go where no OS has gone before

Apps For Good, London, June 29th 2012 Slide Hello24 Ltd. (c) 201234

LLVM & Clang

Page 46: To swiftly go where no OS has gone before

Apps For Good, London, June 29th 2012 Slide Hello24 Ltd. (c) 201234

LLVM & Clang

gcc => llvm-gcc => llvm

Page 47: To swiftly go where no OS has gone before

Swift

Page 48: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide

Objective-C without the C

36

Swift

Page 49: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide

Objective-C without the C

36

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

Swift

Page 50: To swiftly go where no OS has gone before

Swift

Page 51: To swiftly go where no OS has gone before

Safety

Page 52: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 39

Safety - constants vs variables

Page 53: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 39

Safety - constants vs variables

let theAnswer = 42

Page 54: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 39

Safety - constants vs variables

let theAnswer = 42

var numberBooks = 1

Page 55: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 39

Safety - constants vs variables

let theAnswer = 42

var numberBooks = 1

var numberBooks: Int = 1

Page 56: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

Page 57: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

var numberBooks = 1

Page 58: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

var numberBooks = 1

numberBooks = 2.5

Page 59: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

var numberBooks = 1

numberBooks = 2.5 ❗️

Page 60: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

var numberBooks = 1

numberBooks = 2.5 ❗️

numberBooks = Int(2.5)

Page 61: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 40

Safety - no implicit conversion

var numberBooks = 1

numberBooks = 2.5 ❗️

numberBooks = Int(2.5) ✔

Page 62: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 41

Safety - switch

switch index { case 1: println("first") case 2, 3: println("second or third") case 4...10: println("top 10") default: println("others...") }

Page 63: To swiftly go where no OS has gone before

New idioms

Page 64: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 43

Tuples

Page 65: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 43

Tuples

let http404Error = (404, "Not Found")

Page 66: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 43

Tuples

let x = 1 let y = 2

let point = (x, y)

let http404Error = (404, "Not Found")

Page 67: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 43

Tuples

let x = 1 let y = 2

let point = (x, y)

point.0

let http404Error = (404, "Not Found")

Page 68: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 43

Tuples

let x = 1 let y = 2

let point = (x, y)

point.0

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

let http404Error = (404, "Not Found")

Page 69: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 44

Tuples

Page 70: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 44

Tuples// Original var fibonacci = 1 var prev = 0

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

Page 71: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 44

Tuples// Original var fibonacci = 1 var prev = 0

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

// tuples var fibonacci_t = 1 var prev_t = 0

while fibonacci_t < 100 { (prev_t, fibonacci_t) = (fibonacci_t, fibonacci_t + prev_t) println(fibonacci_t) }

Page 72: To swiftly go where no OS has gone before

Functions as 1st class citizens

Page 73: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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

Page 74: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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

Page 75: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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

Page 76: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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

Page 77: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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")

Page 78: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 46

Functions

func 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 79: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

Closures

Page 80: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

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

Page 81: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

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

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

Page 82: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

Closuresfunc 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)" })

Page 83: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

Closuresfunc 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") { name in return "Good morning \(name)" }

Page 84: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 47

Closuresfunc 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") { name in return "Good morning \(name)" }

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

Page 85: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 48

Page 86: To swiftly go where no OS has gone before

Tools

Page 87: To swiftly go where no OS has gone before
Page 88: To swiftly go where no OS has gone before

Playgrounds

Page 89: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201452

New Playground

Page 90: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201453

Empty Playground

Page 91: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201454

Playground

Page 92: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201455

Playground

Page 93: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201455

Playground

Page 94: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201455

Playground

Page 95: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201456

Fibonacci

Page 96: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201457

Fibonacci

Page 97: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201458

Fibonacci

Page 98: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201459

Playground - Timeline

Page 99: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201460

Playgrounds

Page 100: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 2014

‣ Interactive experience

‣ Immediate feedback

‣ Watch code progression through loops

60

Playgrounds

Page 101: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 2014

‣ Interactive experience

‣ Immediate feedback

‣ Watch code progression through loops

‣ Easy way to

‣ prototype

‣ test snippets of code

60

Playgrounds

Page 102: To swiftly go where no OS has gone before

Module 01 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.

60

Playgrounds

Page 103: To swiftly go where no OS has gone before

Swift REPL

Page 104: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201462

REPLRead–eval–print loop

Page 105: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201462

REPL

$ which swift

/usr/bin/swift

Read–eval–print loop

Page 106: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201462

REPL

$ which swift

/usr/bin/swift

$ swift -v

Swift version 1.0 (swift-600.0.51.3)

Target: x86_64-apple-darwin14.0.0

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not

support Swift

Read–eval–print loop

Page 107: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201463

man swift

Page 108: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201464

REPL

Page 109: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201464

REPL$ swift -v

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

Page 110: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201464

REPL$ swift -v

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

Page 111: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201464

REPL$ swift -v

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

Page 112: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201465

REPL

Page 113: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201465

REPL$ swift -v

Page 114: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201465

REPL$ swift -v

Swift version 1.1 (swift-600.0.54.4) Target: x86_64-apple-darwin14.0.0 /Applications/Xcode-Beta.app/Contents/Developer/usr/bin/lldb "--repl=-target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -color-diagnostics"

Page 115: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201465

REPL$ swift -v

Swift version 1.1 (swift-600.0.54.4) Target: x86_64-apple-darwin14.0.0 /Applications/Xcode-Beta.app/Contents/Developer/usr/bin/lldb "--repl=-target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -color-diagnostics"

$ swift

Welcome to Swift! Type :help for assistance.

Page 116: To swiftly go where no OS has gone before

Module 01 Slide Hello24 Ltd. (c) 201466

REPL

$ 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

Page 117: To swiftly go where no OS has gone before

When?

Page 118: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide

‣ 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

68

Now!

Page 119: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 69

Resources

Page 120: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 70

Resources

Page 121: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 71

My Book

[email protected]

Page 122: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 72

Page 123: To swiftly go where no OS has gone before

How?

Page 124: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 74

Swift project

Page 125: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 75

Bridging

Page 126: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 76

Bridging

Page 127: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 77

// // STDataObject.swift //

import Foundation import CoreData

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 128: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 78

Page 129: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 78

Page 130: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 79

Page 131: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 79

Page 132: To swiftly go where no OS has gone before

Objective-C ➾ Swift

Page 133: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 81

Fizz Buzz classic

Page 134: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 81

Fizz Buzz classic

let fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })

fizzBuzz(1) fizzBuzz(2) fizzBuzz(3) fizzBuzz(5) fizzBuzz(9) fizzBuzz(10) fizzBuzz(15)

Page 135: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 81

Fizz Buzz classic

let fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })

fizzBuzz(1) fizzBuzz(2) fizzBuzz(3) fizzBuzz(5) fizzBuzz(9) fizzBuzz(10) fizzBuzz(15)

“1”“2”“fizz”“buzz”“fizz”“buzz”“fizzbuzz”

Page 136: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 82

Swift Fizz Buzz

Page 137: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 82

Swift Fizz Buzzlet swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })

swiftFizzBuzz(1) swiftFizzBuzz(2) swiftFizzBuzz(3) swiftFizzBuzz(5) swiftFizzBuzz(9) swiftFizzBuzz(10) swiftFizzBuzz(15)

Page 138: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 82

Swift Fizz Buzzlet swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })

swiftFizzBuzz(1) swiftFizzBuzz(2) swiftFizzBuzz(3) swiftFizzBuzz(5) swiftFizzBuzz(9) swiftFizzBuzz(10) swiftFizzBuzz(15)

“1”“2”“fizz”“buzz”“fizz”“buzz”“fizzbuzz”

Page 139: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 83

Fizz Buzz - side by sidelet fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })

let swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })

Page 140: To swiftly go where no OS has gone before

Objective-C ➾ Swift

Page 141: To swiftly go where no OS has gone before

CuriosMinds, Brasov, Sept 18th 2014 Slide 85

Page 142: To swiftly go where no OS has gone before

Copyleft 2014 me

Thank you!

Paul Ardeleanu

[email protected] @pardel