Unexpected behaviour of Property Observer
| Originator: | lars.lockefeer | ||
| Number: | rdar://23200188 | Date Originated: | 21-10-2015 |
| Status: | Resolved: | ||
| Product: | Developer Tools | Product Version: | Xcode Version 7.0 (7A218) |
| Classification: | Other Bug | Reproducible: | Always |
Summary: I ran into an unexpected (for me) behaviour of property observers today, where a property observer is triggered whenever you access a computed property of the property for which the observer is added. Say you have a protocol A which defines a get property x. You then create a class AImpl, in which you conform to the protocol by means of a computed property. Now, in yet another class B, you define a property p of type A and give it a didSet property observer. Observed behaviour: Whenever you access p inside a member function of A, the property observer will be triggered. Expected behaviour: Whenever you access p inside a member function of A, the property observer will NOT be triggered. If instead we define a property q of type Aimpl, the observed behaviour is as expected: the property observer will NOT be triggered whenever you access q inside a member function of A. See the attached playground for a reproducible case. Steps to Reproduce: 1. Open the Playground 2. Run the Playground Expected Results: The text “Observed property observer called” is printed only once: when we set the property/ Actual Results: The text “Observed property observer called” is printed twice: when we set the property as well as when we access it. Notes: Example playground can also be found here: https://github.com/larslockefeer/Radars/blob/master/UnexpectedPropertyObserverBehaviour.playground/Contents.swift
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!