Swift: how to change a property's value without calling its didSet function

后端 未结 4 490
谎友^
谎友^ 2021-01-01 12:41

How can you set a property\'s value in Swift, without calling its didSet() function outside of an initialization context? The code below was a failed experimen

4条回答
  •  情书的邮戳
    2021-01-01 13:11

    The issue I was trying to solve is I wanted to get a doCommonUpdate call when one value changed, or one if both changed at the same time. Doing this created a recursion because if either value changed it would call didSet each time. Example setup, mine was more involved:

    class Person {
        var first: String = "" {
            didSet {
                updateValues(first: first, last: last)
            }
        }
        var last: String = "" {
            didSet {
                updateValues(first: first, last: last)
            }
        }
        init() {}
        init(first: String, last: String) {
            self.first = first
            self.last = last
        }
        // also want to set both at the same time.
        func updateValues(first: String, last: String) {
    //        self.first = first // causes recursion.
    //        self.last = last
            doCommonSetup(first: first, last: last)
        }
        func doCommonSetup(first: String, last: String) {
            print(first, last)
        }
    }
    let l = Person()
    l.first = "Betty"
    l.last = "Rubble"
    _ = Person(first: "Wilma", last: "Flintstone")
    
    > Betty 
    > Betty Rubble
    

    Note Wilma does not print because of the commented out lines.

    My solution was to move all those variables into a separate struct. The this approach solves the problem and has the side benefit of creating a another grouping which helps meaning. We still get the doCommonSetup when either value changes independently and when we get both values changed at the same time.

    class Person2 {
        struct Name {
            let first: String
            let last: String
        }
        var name: Name
        init() {
            name = Name(first: "", last: "")
        }
        init(first: String, last: String) {
            name = Name(first: first, last: last)
            updateValues(name: name)
        }
        var first: String = "" {
            didSet {
                name = Name(first: first, last: self.last)
                updateValues(name: name)
            }
        }
        var last: String = "" {
            didSet {
                name = Name(first: self.first, last: last)
                updateValues(name: name)
            }
        }
        // also want to set both at the same time.
        func updateValues(name: Name) {
            self.name = name
            doCommonSetup(name: name)
        }
        func doCommonSetup(name: Name) {
            print(name.first, name.last)
        }
    }
    
    let p = Person2()
    p.first = "Barney"
    p.last = "Rubble"
    _ = Person2(first: "Fred", last: "Flintstone")
    
    > Barney 
    > Barney Rubble
    > Fred Flintstone
    

提交回复
热议问题