Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Values

Swift Values

Presented at iOS Love Meetup Group

Francisco Díaz

July 23, 2015
Tweet

More Decks by Francisco Díaz

Other Decks in Programming

Transcript

  1. Swift Values

    View full-size slide

  2. Francisco Díaz
    @fco_diaz

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. 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}

    View full-size slide

  7. Reference Type

    View full-size slide

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

    View full-size slide

  9. 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"}

    View full-size slide

  10. Value Types are Immutable

    View full-size slide

  11. 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}

    View full-size slide

  12. 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'

    View full-size slide

  13. Using Value Types

    View full-size slide

  14. The Value layer game
    by Andy Matuschak

    View full-size slide

  15. Object layer
    Value layer

    View full-size slide

  16. Prefer structs over classes

    View full-size slide

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

    View full-size slide

  18. 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"))

    View full-size slide

  19. Every Value type should be Equatable

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. 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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. 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()
    ...
    }

    View full-size slide

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

    View full-size slide

  29. Only update what changed

    View full-size slide

  30. But how do I know what
    changed?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. 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

    View full-size slide

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

    View full-size slide