<div dir="ltr">I don't know... when didSet is called the struct is already mutated so I would expect all the references to be updated as well.<div><br></div><div>Regarding the capture in the closure it should always capture a reference to "myClass", and call the accessors in the moment the closure is executed. Adding</div><div>
<p class=""><span class="">myClass</span><span class="">.</span><span class="">myString</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">"3"</span></p><p class=""><span class="">will print "2".</span></p><p class=""><span class="">because </span>myClass.myString still holds the previous struct.</p><p class="">I will solve this problem by making Observable a reference type, but I was wondering if someone could share more details about this behaviour.</p>Cheers,<br>Diego.<br><p class=""><span class=""><br></span></p><p class=""><br></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-07-14 5:19 GMT+02:00 Zhao Xin <span dir="ltr"><<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">I think the order is right.</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">value.set >> value.didSet >> string.set >> string.didSet</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">you expected </font><span style="font-family:georgia,serif">value.set >> string.set >> </span><span style="font-family:georgia,serif"> </span><span style="font-family:georgia,serif">value.didSet >></span><span style="font-family:georgia,serif"> </span><span style="font-family:georgia,serif">string.didSet is not correct.</span></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">The value "1" is not you expected. However, that is something that I think is tricky. For closure</font></div><div class="gmail_default"><font face="georgia, serif"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> {<br> print(myClass.myString.value)<br>}</blockquote><div>it captured the value. However, unless Objective-C, Swift will decide if it is a static capture or an inout capture. So it should be consider right to be "1" or "2"? I am not sure about that.</div><div><br></div><div>Zhaoxin</div></font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, Jul 14, 2016 at 8:36 AM, Diego Sánchez <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Hi all,<div><br></div><div>The following snippet summarises an issue I was investigating:</div><div><br></div><div><div>struct Observable<T> {</div><div> var value: T {</div><div> didSet {</div><div> print("Observable.didSet")</div><div> callback?()</div><div> }</div><div> }</div><div> var callback: (() -> Void)?</div><div>}</div><div><br></div><div>class MyClass {</div><div> var myString: Observable<String> {</div><div> get {</div><div> return _myString</div><div> }</div><div> set {</div><div> print("MyClass.Setter")</div><div> self._myString = newValue</div><div> }</div><div> }</div><div><br></div><div> private var _myString: Observable<String></div><div> init (string: Observable<String>) {</div><div> self._myString = string</div><div> print("MyClass.init.end")</div><div> }</div><div>}</div><div><br></div><div>let myClass = MyClass(string: Observable<String>(value: "1", callback: nil))</div><div>myClass.myString.callback = {</div><div> print(myClass.myString.value)</div><div>}</div><div>myClass.myString.value = "2"</div></div><div><br></div><div><b>Output:</b></div><div><div>MyClass.init.end</div><div>MyClass.Setter</div><div>Observable.didSet</div><div><b>1</b></div><div>MyClass.Setter</div></div><div><br></div><div>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.</div><div><br></div><div>Is it feasible that all the <b>set</b>s are called first, and then all the <b>didSet</b>s?</div><div><br></div><div>Cheers,</div><div>Diego</div></div>
<br></div></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>