views:

417

answers:

3

I've attempted to observe the (readonly) visibileViewController property of a UINavigationController with no success. I was able to successfully observe a readwrite property I defined myself for testing purposes on another class.

Is it possible to observer readonly attributes?

A: 

This is definitely be possible using NSKeyValueObserving. Properties actually have getter/setter implementations, they are just done for you by the compiler via the @synthesize keyword in an Objective-C classes implementation. Since the key-value observing protocol is based on the standard getter/setter conventions in Objective-C, observing properties works fine. The documentation (linked above) even mentions class properties by name:

"The NSKeyValueObserving (KVO) informal protocol defines a mechanism that allows objects to be notified of changes to the specified properties of other objects."

Nick Haddad
+2  A: 

Yes, it is possible to observe read-only properties. However, if the object that declares that property makes changes to the value of that property in a way that is not Key-Value Observing compliant (e.g. changes the value of the instance variable backing that property directly without seding willChangeValueForKey: and didChangeValueForKey: notifications) then observers will not automatically be notified by the KVO system. If you can verify that the value of this property is changing and your observers are not being notified, I would (1) post some code here or elsewhere so that others can help you find your bug and (2) if there is no bug in your code, file a bug on Apple's radar.

Barry Wark
A: 

You can certainly observe readonly properties but be aware that in order for KVO to work you need to be KVC compliant - which means using either the setter/getter for a property (since you're readonly, you don't get a setter for free via @synthesize) or the property's -setValue:forKey: method.

wisequark