[swift-users] didSet and time when propagation of mutation happens
Diego Sánchez
diego.sanchezr at gmail.com
Wed Jul 13 19:36:29 CDT 2016
Hi all,
The following snippet summarises an issue I was investigating:
struct Observable<T> {
var value: T {
didSet {
print("Observable.didSet")
callback?()
}
}
var callback: (() -> Void)?
}
class MyClass {
var myString: Observable<String> {
get {
return _myString
}
set {
print("MyClass.Setter")
self._myString = newValue
}
}
private var _myString: Observable<String>
init (string: Observable<String>) {
self._myString = string
print("MyClass.init.end")
}
}
let myClass = MyClass(string: Observable<String>(value: "1", callback: nil))
myClass.myString.callback = {
print(myClass.myString.value)
}
myClass.myString.value = "2"
*Output:*
MyClass.init.end
MyClass.Setter
Observable.didSet
*1*
MyClass.Setter
Obviously I wasn't expecting to get "1", but "2" in the callback's print,
and this happens because MyClass.setter is called after Observable.didSet
completes.
Is it feasible that all the *set*s are called first, and then all the
*didSet*s?
Cheers,
Diego
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160714/4fe9f663/attachment.html>
More information about the swift-users
mailing list