<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">didSet is called as soon as the property is stored… Excepted when the value is stored in the initialiser code.</div><div class=""><br class=""></div><div class="">For me it is completely predictable that your code enter an infinite loop</div><div class=""><br class=""></div><div class="">Regards</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 4 Sep 2016, at 17:11, adelzhang via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Thanks for reply.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">How does Swift choose *rules* as you said?</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">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 style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> class Base {<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> var a: Int = 0<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> }<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> class Child : Base {<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> override var a: Int {<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> didSet {<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> a = a + 1<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> }<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> }<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> }</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> <span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> let child = Child()<span class="Apple-converted-space"> </span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""> child.a = 3</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Any differcen with situation 1? </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">在 Sun, 04 Sep 2016 20:12:42 +0800,Zhao Xin <<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>> 写道:<br class=""></div><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0.8ex; border-left-color: rgb(0, 0, 255); border-left-width: 2px; border-left-style: solid; padding-left: 1ex;" class=""><div dir="ltr" class=""><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 class=""><br class=""></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 class=""><br class=""></div><div class="">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 class=""><br class=""></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;" class=""></span><span style="font-family: arial, sans-serif; font-size: 13px;" class="">2) infinite loop</span></blockquote><div class=""><br class=""></div><div class="">This can't apply the above rule as they set each other, causing the infinite loops. </div><div class=""><br class=""></div><div class="">Zhaoxin</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""><div class="gmail_quote">On Sun, Sep 4, 2016 at 7:41 PM, Zhao Xin<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:owenzx@gmail.com" target="_blank" class="">owenzx@gmail.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br 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;"><div dir="ltr" class=""><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;" class="">1) when `didSet` observer will call?</span></blockquote><div class=""><br class=""></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 class=""></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;" class="">2) infinite loop</span></blockquote><br style="font-size: 13px;" class=""><div class=""><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 class=""></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;" class="">3) override property observer</span></blockquote><div class=""> </div><div class=""><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 class=""></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 class=""></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 class=""></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 class=""></div><div class="gmail_default" style="font-family: georgia, serif;">From: Apple Inc. “The Swift Programming Language (Swift 3 Beta)”。 iBooks.<span class="Apple-converted-space"> </span><a href="https://itun.es/us/k5SW7.l" target="_blank" class="">https://itun.es/us/k5SW7.l</a></div><div class="gmail_default" style="font-family: georgia, serif;"><br class=""></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 class=""></div><div class="gmail_default" style="font-family: georgia, serif;">Last, </div><div class="gmail_default" style="font-family: georgia, serif;"><br class=""></div><div class="gmail_default"><span class=""><div class="gmail_default"><font face="georgia, serif" class=""> let base = child as Base</font></div><div class="gmail_default"><font face="georgia, serif" class=""> base.a = 4 // still output "base didset" and "child didset"</font></div><div class="gmail_default"><font face="georgia, serif" class=""><br class=""></font></div></span><div class="gmail_default"><font face="georgia, serif" class="">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;" class="">Child`.</span></div></div><div class="gmail_default" style="font-family: georgia, serif;"><br class=""></div><div class="gmail_default" style="font-family: georgia, serif;">Zhaoxin</div></div><br class=""></div><br class=""></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sun, Sep 4, 2016 at 6:25 PM, adelzhang via swift-users<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br 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;">Hi all<br class=""><br class="">It sounds convenient to monitor change in property's value using property observer.<br class="">But TSPL(The Swift Programming Language) talk little about property observer. There<br class="">are some questions abouts property observer.<br class=""><br class="">1) when `didSet` observer will call?<br class=""><br class="">I assume it's fine that changing property's value in `didSet` observer.<br class=""><br class=""> class Foo {<br class=""> var a: Int = 0 {<br class=""> didSet {<br class=""> print("didset")<br class=""> a = a + 1<br class=""> }<br class=""> }<br class=""> }<br class=""><br class=""> let foo = Foo()<br class=""> foo.a = 4 // only output "didset" once<br class=""><br class="">Why it don't cause infinite loop?<br class=""><br class="">2) infinite loop<br class=""><br class=""> // this code snippet cause inifinite loop<br class=""> class Foo {<br class=""> var a: Int = 0 {<br class=""> didSet {<br class=""> b = a + 1<br class=""> }<br class=""> }<br class=""><br class=""> var b: Int = 1 {<br class=""> didSet {<br class=""> a = b - 1<br class=""> }<br class=""> }<br class=""> }<br class=""><br class=""> let foo = Foo()<br class=""> foo.a = 2<br class=""><br class="">3) override property observer<br class=""><br class=""> class Base {<br class=""> var a: Int = 0 {<br class=""> didSet {<br class=""> print("base didset")<br class=""> }<br class=""> }<br class=""> }<br class=""><br class=""> class Child : Base {<br class=""> override var a : Int {<br class=""> didSet {<br class=""> print("child didset")<br class=""> }<br class=""> }<br class=""> }<br class=""><br class=""> let child = Child()<br class=""> child.a = 2 // output "base didset" and "child didset"<br class=""> let base = child as Base<br class=""> base.a = 4 // still output "base didset" and "child didset"<br class=""><br class="">Why overriding property observer still call parent's `didSet` observer?<br class=""><br class="">--<br class="">Adel<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div><blockquote style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0.8ex; border-left-color: rgb(0, 0, 255); border-left-width: 2px; border-left-style: solid; padding-left: 1ex;" class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-users mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span></div></blockquote></div><br class=""></div></body></html>