Re: [swift-users] strange property observer behavior

2016-09-04 Thread adelzhang via swift-users
Hi, The following code works fine. The property `a` is stored twice. But it don't enter infinite loop. class Foo { var a: Int = 0 { didSet { a = a + 1 } } } let foo = Foo() foo.a = 2 print(foo.a) // output 3 Regard

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
It seems like a bug. You should file it. Zhaoxin On Sun, Sep 4, 2016 at 11:11 PM, wrote: > Thanks for reply. > > How does Swift choose *rules* as you said? > > Swfit encourage to override the property observer. But when we change the > own property in Child class's `didSet` observer, that would

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Jens Alfke via swift-users
> On Sep 4, 2016, at 8:11 AM, adelzhang via swift-users > wrote: > > 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: I’m not a Swift guru, but that seems like a bug to me. The book exp

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Gerard Iglesias via swift-users
Hi, didSet is called as soon as the property is stored… Excepted when the value is stored in the initialiser code. For me it is completely predictable that your code enter an infinite loop Regards > On 4 Sep 2016, at 17:11, adelzhang via swift-users > wrote: > > Thanks for reply. > > How

Re: [swift-users] strange property observer behavior

2016-09-04 Thread adelzhang via swift-users
Thanks for reply. How does Swift choose *rules* as you said? 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: class Base { var a: Int = 0 } class Child : Base {

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
> > 1) when `didSet` observer will call? ​For me, it is more like Swift developer tries to override some beginner's > flaw. 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. ​2) infini

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
> > 1) when `didSet` observer will call? ​For me, it is more like Swift developer tries to override some beginner's flaw. ​2) infinite loop ​If you intended to do things bad, things ​went bad. 3) override property observer ​You mentioned "TSPL(The Swift Programming Language) ​", and it says

[swift-users] strange property observer behavior

2016-09-04 Thread adelzhang via swift-users
Hi all It sounds convenient to monitor change in property's value using property observer. But TSPL(The Swift Programming Language) talk little about property observer. There are some questions abouts property observer. 1) when `didSet` observer will call? I assume it's fine that changing