Slide 1

Slide 1 text

Swift Values

Slide 2

Slide 2 text

Francisco Díaz @fco_diaz

Slide 3

Slide 3 text

Objective-C -> Swift NSArray / NSMutableArray -> Array NSDictionary / NSMutableDictionary -> Dictionary NSString / NSMutableString -> String

Slide 4

Slide 4 text

4 Differences between Value / Reference Types 4 Immutability in Swift 4 Using Value Types

Slide 5

Slide 5 text

Value Type

Slide 6

Slide 6 text

Struct struct Point { var x: Int, y: Int }

Slide 7

Slide 7 text

Copying creates an independent instance with its own unique copy of its data var a = Point(x: 1, y: 2) var b = a // a: {1, 2}; b: {1, 2} b.x = 3 // a: {1, 2}; b: {3, 2}

Slide 8

Slide 8 text

Reference Type

Slide 9

Slide 9 text

Class class Person { var name: String init(name: String) { self.name = name } }

Slide 10

Slide 10 text

Copying a reference, on the other hand, implicitly creates a shared instance let pedro = Person(name: "Pedro") var clon = pedro // pedro: {"Pedro"}; clon: {"Pedro"} clon.name = "Pablo" // pedro: {"Pablo"}; clon: {"Pablo"}

Slide 11

Slide 11 text

Value Types are Immutable

Slide 12

Slide 12 text

What about variables? And mutating functions? Eh? Eh? ! struct Point { var x: Int, y: Int init(x: Int, y: Int) { self.x = x self.y = y } mutating func movePointBy(x: Int, y: Int) { self.x += x self.y += y } } var a = Point(x: 1, y: 2) a.movePointBy(3, y: 3) // a: {4, 5} a.x = 20 // a: {20, 5}

Slide 13

Slide 13 text

let a = Point(x: 1, y: 2) a.movePointBy(3, y: 3) // Compilation error a.x = 20 // Compilation error // Immutable value of type 'Point' only has mutating members named 'movePointBy'

Slide 14

Slide 14 text

Using Value Types

Slide 15

Slide 15 text

The Value layer game by Andy Matuschak

Slide 16

Slide 16 text

Object layer Value layer

Slide 17

Slide 17 text

Prefer structs over classes

Slide 18

Slide 18 text

Constants by default struct Point { let x: Int, y: Int }

Slide 19

Slide 19 text

Use mutability carefully, where it makes sense struct Meetup { let speakers: [String] } struct Meetup { var speakers: [String] mutating func addAwesomeSpeaker(speaker: String) } addAwesomeSpeaker("Francisco") ~== Meetup(speaker: speakers.append("Francisco"))

Slide 20

Slide 20 text

Every Value type should be Equatable

Slide 21

Slide 21 text

Values are inherently equatable let a = "Hola " let b = "Mundo" a == b // false "Hola Mundo" == a + b // true 1 == 2 - 1 // true

Slide 22

Slide 22 text

How? struct Point: Equatable { let x: Int, y: Int } func ==(lhs: Point, rhs: Point) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y }

Slide 23

Slide 23 text

When to use Classes? 4 NetworkController1 == NetworkController2 ??? 4 UIKit

Slide 24

Slide 24 text

Example

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

MVVM Board Model: Contains data representing a board Board VM: Communicates between Model and View - Converts model data to be displayed - Takes user input and acts on model Board View: Displays a board to the user Game Scene: Puts it all together.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Choose immutability and see where it takes you. 1 Rich Hickey

Slide 30

Slide 30 text

struct Board { let cells: [Cell] } struct Cell { let value: Int }

Slide 31

Slide 31 text

struct BoardViewModel { let board: Board func cellViewModelAtIndex(index: Int) -> CellViewModel } struct CellViewModel { let cell: Cell let index: Int func attributes() -> (color: UIColor, texture: SKTexture, ...) }

Slide 32

Slide 32 text

class BoardView: SKSpriteNode { var cellViews: [CellView] = [] init(size: CGSize) func configure(boardViewModel: BoardViewModel) } class CellView: SKSpriteNode { init(size: CGSize, cellViewModel: CellViewModel) func configure(cellViewModel: CellViewModel) func scaleBy(scale: CGFloat) func animateTouch() ... }

Slide 33

Slide 33 text

Every time something changes, create a new BoardViewModel and pass it to the BoardView func configure(boardViewModel: BoardViewModel)

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Only update what changed

Slide 36

Slide 36 text

But how do I know what changed?

Slide 37

Slide 37 text

They're Values ...and you can compare values easily func configure(cellViewModel: CellViewModel) { if oldCellViewModel != cellViewModel { // update } }

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

4 Differences between Value / Reference Types 4 Immutability in Swift 4 Using Value Types

Slide 40

Slide 40 text

Resources: Swift Blog: Value and Reference Types Should I use a Swift struct or a class? WWDC 2015: Session 408 WWDC 2015: Session 414 The Value of Values by Rich Hickey Enemy of the State by Justin Spahr-Summers Functioning as a Functionalist by Andy Matuschak Immutable Data and React by Lee Byron

Slide 41

Slide 41 text

Thanks! Slides available at: https://github.com/fdiaz/swift-values-talk