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 Slide

  2. Francisco Díaz
    @fco_diaz

    View Slide

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

    View Slide

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

    View Slide

  5. Value Type

    View Slide

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

    View Slide

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

  8. Reference Type

    View Slide

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

    View Slide

  10. 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 Slide

  11. Value Types are Immutable

    View Slide

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

  13. 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 Slide

  14. Using Value Types

    View Slide

  15. The Value layer game
    by Andy Matuschak

    View Slide

  16. Object layer
    Value layer

    View Slide

  17. Prefer structs over classes

    View Slide

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

    View Slide

  19. 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 Slide

  20. Every Value type should be Equatable

    View Slide

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

    View Slide

  22. 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 Slide

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

    View Slide

  24. Example

    View Slide

  25. View Slide

  26. View Slide

  27. 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 Slide

  28. View Slide

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

    View Slide

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

    View Slide

  31. 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 Slide

  32. 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 Slide

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

    View Slide

  34. View Slide

  35. Only update what changed

    View Slide

  36. But how do I know what
    changed?

    View Slide

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

    View Slide

  38. View Slide

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

    View Slide

  40. 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 Slide

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

    View Slide