[swift-users] Struct, Mutating and Property Observers

Седых Александр se-a at mail.ru
Fri Oct 7 08:20:15 CDT 2016


Please look at that code:

class Mut {
    
    var zetstruct = MutStr(z: 5) {
        didSet {
            print("struct was change")
            zetstruct.addZet(num: 10)
        }
    }
    
    struct MutStr {
        var z: Int
        mutating func addZet(num: Int) {
            z += num
        }
    }
    
    func work() {
        zetstruct.addZet(num: 10)
    }
}

let mut = Mut()
mut.work()

Why in this case we don't have infinity cycle? Because when we change struct's property within mutating function it cause that instance property 'zetstruct' change and didSet invocate.

-- 
Седых Александр


More information about the swift-users mailing list