<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">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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">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>