Slide 1

Slide 1 text

Unleashing Swift

Slide 2

Slide 2 text

@iwantmyrealname

Slide 3

Slide 3 text

Choose your own Adventure

Slide 4

Slide 4 text

swift

Slide 5

Slide 5 text

fast modern safe interactive

Slide 6

Slide 6 text

Strong typing Mutability Value Types ARC

Slide 7

Slide 7 text

Some code var arr = [3, 4, 1, 6, 2, 5]; arr.sort(); arr; //= ?? What's the langauge? and the result?

Slide 8

Slide 8 text

Javascript var arr = [3, 4, 1, 6, 2, 5]; arr.sort(); arr; //= [1, 2, 3, 4, 5, 6]

Slide 9

Slide 9 text

Swift var arr = [3, 4, 1, 6, 2, 5]; arr.sort(); arr; //= [3, 4, 1, 6, 2, 5]

Slide 10

Slide 10 text

getting started

Slide 11

Slide 11 text

Getting Started — Mutability — Value types — Classes / Structs — Functions — Extensions — Closures

Slide 12

Slide 12 text

Mutability let twelve = 12 //twelve = 13 var thirteen = 13 thirteen = 12 //thirteen = 13.0

Slide 13

Slide 13 text

Value Types var title = "This is a string" var secondTitle = title secondTitle += ", extended" print(title) // "This is a string" print(secondTitle) // "This is a string, extended"

Slide 14

Slide 14 text

Classes — Reference types — Inheritance / Dynamic Dispatch class Vehicle { var maxSpeed: Double } class Car: Vehicle { var numberOfWheels: Int }

Slide 15

Slide 15 text

Structs — Value types — No inheritance — Immutable by default struct Point3D { let x: Double let y: Double let x: Double }

Slide 16

Slide 16 text

Class Initialisation 1. All stored properties 2. super.init() / self.init() 3. Change inherited properties 4. Call instance methods / access self

Slide 17

Slide 17 text

Functions func square(value: Int) -> Int { return value * value } func exp(value: Int, power: Int) -> Int { return power > 0 ? value * exp(value, power: power-1) : 1 } func curryPower(power: Int)(_ value: Int) -> Int { return exponentiate(value, power: power) } let cube = curryPower(3) cube(3) //= 27

Slide 18

Slide 18 text

Extensions — Add functionality to types extension Int { func toThePower(power: Int) -> Int { return exponentiate(self, power: power) } } 2.toThePower(5)

Slide 19

Slide 19 text

Closures typealias Callback = (reply: String) -> () func asyncHello(name: String, callback: Callback) { callback(reply: "Hello \(name)") }

Slide 20

Slide 20 text

Functional Tinge — Functions are 1st class — map, reduce, filter on CollectionType — TLO implemented let scores = [1, 2, 4, 5, 3, 5] let cubedScores = scores.map { cube($0) } //= [1, 8, 64, 125, 27, 125]

Slide 21

Slide 21 text

"modern" concepts

Slide 22

Slide 22 text

Modern Concepts — Enums — Pattern Matching — Optionals — Protocols

Slide 23

Slide 23 text

Enums — Raw values — Associated values — Equivalent to mapping || — Recursive enums enum Activity: String { case Running case Swimming case Cycling }

Slide 24

Slide 24 text

Pattern Matching — Concept from functional languages — Part of if and switch func evaluate(expresion: ArithmeticExpression) -> Int { switch expresion { case .Number(let value): return value case .Addition(let left, let right): return evaluate(left) + evaluate(right) case .Multiplication(let left, let right): return evaluate(left) * evaluate(right) } }

Slide 25

Slide 25 text

Optionals — Handling of nil — Just an enum with some syntactic sugar enum Optional { case None case Some(Wrapped) }

Slide 26

Slide 26 text

?!??

Slide 27

Slide 27 text

Protocols The problems with classes: — Inheritance — Implicit sharing — Lose type relationships

Slide 28

Slide 28 text

Protocols (a.k.a. Interfaces) — Supports value types — Can add functionality to existing types — No instance data available — Can include default implementations

Slide 29

Slide 29 text

Other Stuff

Slide 30

Slide 30 text

Other Stuff — Interoperability — Tuples — Generics — Error Handling — Playgrounds

Slide 31

Slide 31 text

the future

Slide 32

Slide 32 text

Open source

Slide 33

Slide 33 text

how can I play?

Slide 34

Slide 34 text

buy a proper computer

Slide 35

Slide 35 text

or wait

Slide 36

Slide 36 text

you become a better programmer by playing with other lanugages — Me, just now

Slide 37

Slide 37 text

go forth and play @iwantmyrealname github.com/sammyd