[swift-evolution] [swift-users] Struct, Mutating and Property Observers

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Oct 7 11:20:55 CDT 2016


From the Swift book we know this:

If you assign a value to a property within its own didSet observer, the new value that you assign replaces the one that was just set.
That means that it won’t fire another didSet observer due replacing or in your case mutating.



-- 
Adrian Zubarev
Sent with Airmail

Am 7. Oktober 2016 um 17:46:37, Седых Александр via swift-users (swift-users at swift.org) schrieb:

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.

--  
Седых Александр
_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161007/0c5a0645/attachment.html>


More information about the swift-evolution mailing list