<div dir="ltr"><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><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">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"><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><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="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>
______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/swift-users</a><br>
</blockquote></div><br></div>