<!DOCTYPE html><html><head>
<style type="text/css">body { font-family:'Helvetica'; font-size:12px}</style>
</head>
<body><div>Thanks for reply.
</div><div><br></div><div>How does Swift choose *rules* as you said?
</div><div><br></div><div>Swfit encourage to override the property observer. But when we change the own property in Child class's `didSet` observer,
that would cause infinite loop:
</div><div><br></div><div> class Base {
</div><div> var a: Int = 0
</div><div> }
</div><div><br></div><div> class Child : Base {
</div><div> override var a: Int {
</div><div> didSet {
</div><div> a = a + 1
</div><div> }
</div><div> }
</div><div> }</div><div> </div><div> let child = Child()
</div><div> child.a = 3
</div><div><br></div><div>Any differcen with situation 1? </div><div><br></div><div><br></div><div>在 Sun, 04 Sep 2016 20:12:42 +0800,Zhao Xin <owenzx@gmail.com> 写道:<br></div><br><blockquote style="margin: 0 0 0.80ex; border-left: #0000FF 2px solid; padding-left: 1ex"><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif"><span class="gmail-im" style="font-family:arial,sans-serif;font-size:13px"><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">1) when `didSet` observer will call?</blockquote><div><br></div></span><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">For me, it is more like Swift developer tries to override some beginner's flaw. </blockquote><div><br></div><div>Above is incorrect. You can change property's value in `didSet`, that won't cause didSet called again as it is intended to give you the opportunity to do that. </div><div><br></div><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"><span style="font-family:arial,sans-serif;font-size:13px"></span><span style="font-family:arial,sans-serif;font-size:13px">2) infinite loop</span></blockquote><div><br></div><div>This can't apply the above rule as they set each other, causing the infinite loops. </div><div><br></div><div>Zhaoxin</div></div></div></blockquote><div><br><div class="gmail_quote">On Sun, Sep 4, 2016 at 7:41 PM, Zhao Xin <span dir="ltr"><<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</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"><span class=""><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"><span style="font-family:arial,sans-serif;font-size:13px">1) when `didSet` observer will call?</span></blockquote><div><br></div></span><div class="gmail_default" style="font-family:georgia,serif">For me, it is more like Swift developer tries to override some beginner's flaw. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><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"><span style="font-size:13px;font-family:arial,sans-serif">2) infinite loop</span></blockquote><br style="font-size:13px"><div><div class="gmail_default" style="font-family:georgia,serif">If you intended to do things bad, things went bad.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><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"><span style="font-family:arial,sans-serif;font-size:13px">3) override property observer</span></blockquote><div> </div><div><div class="gmail_default" style="font-family:georgia,serif">You mentioned "TSPL(The Swift Programming Language) ", and it says in it:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default" style="font-family:georgia,serif">“NOTE</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">The willSet and didSet observers of superclass properties are called when a property is set in a subclass initializer, after the superclass initializer has been called. They are not called while a class is setting its own properties, before the superclass initializer has been called.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">For more information about initializer delegation, see Initializer Delegation for Value Types and Initializer Delegation for Class Types.”</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">From: Apple Inc. “The Swift Programming Language (Swift 3 Beta)”。 iBooks. <a href="https://itun.es/us/k5SW7.l" target="_blank">https://itun.es/us/k5SW7.l</a></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">You didn't provide a `init()`, but since you properties were already set. There was a hidden `init()` when you called `Child()`.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Last, </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><span class=""><div class="gmail_default"><font face="georgia, serif"> let base = child as Base</font></div><div class="gmail_default"><font face="georgia, serif"> base.a = 4 // still output "base didset" and "child didset"</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div></span><div class="gmail_default"><font face="georgia, serif">In Swift, as or as! won't change the instance's dynamic type. So it does nothing. `type(of:base)` is still `</font><span style="font-size:13px">Child`.</span></div></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><br></div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 4, 2016 at 6:25 PM, adelzhang via swift-users <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">Hi all<br>
<br>
It sounds convenient to monitor change in property's value using property observer.<br>
But TSPL(The Swift Programming Language) talk little about property observer. There<br>
are some questions abouts property observer.<br>
<br>
1) when `didSet` observer will call?<br>
<br>
I assume it's fine that changing property's value in `didSet` observer.<br>
<br>
class Foo {<br>
var a: Int = 0 {<br>
didSet {<br>
print("didset")<br>
a = a + 1<br>
}<br>
}<br>
}<br>
<br>
let foo = Foo()<br>
foo.a = 4 // only output "didset" once<br>
<br>
Why it don't cause infinite loop?<br>
<br>
2) infinite loop<br>
<br>
// this code snippet cause inifinite loop<br>
class Foo {<br>
var a: Int = 0 {<br>
didSet {<br>
b = a + 1<br>
}<br>
}<br>
<br>
var b: Int = 1 {<br>
didSet {<br>
a = b - 1<br>
}<br>
}<br>
}<br>
<br>
let foo = Foo()<br>
foo.a = 2<br>
<br>
3) override property observer<br>
<br>
class Base {<br>
var a: Int = 0 {<br>
didSet {<br>
print("base didset")<br>
}<br>
}<br>
}<br>
<br>
class Child : Base {<br>
override var a : Int {<br>
didSet {<br>
print("child didset")<br>
}<br>
}<br>
}<br>
<br>
let child = Child()<br>
child.a = 2 // output "base didset" and "child didset"<br>
let base = child as Base<br>
base.a = 4 // still output "base didset" and "child didset"<br>
<br>
Why overriding property observer still call parent's `didSet` observer?<br>
<br>
--<br>
Adel<br>
<br>
<br>
_______________________________________________<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>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div><blockquote style="margin: 0 0 0.80ex; border-left: #0000FF 2px solid; padding-left: 1ex">
</blockquote><br></body></html>