Re: what's the correct key path to observe for a BOOL property?
Re: what's the correct key path to observe for a BOOL property?
- Subject: Re: what's the correct key path to observe for a BOOL property?
- From: Ken Thomases <email@hidden>
- Date: Thu, 16 Dec 2010 10:29:49 -0600
On Dec 16, 2010, at 7:45 AM, Roland King wrote:
> If I have a property foo and it's boolean and has the getter/setter isFoo/setFoo:, what's the correct key path to observe for KVO, assuming default automatic KVO generation as provided by NSObject, is it 'foo', or is it 'isFoo'?
It's "foo". The key is the name of the property. The names of the methods can be derived from the key via a number of templates described in the KVC and KVO documentation. One of them is "is<Key>", which should make it clear that the "is" is not part of the key.
> It seemed to me that the method which triggers automatic KVO (setFoo:) only knows about its own signature and so would send a KVO notification for key path 'foo'.
Well, setFoo: doesn't have to "know" what key to generate a change notification for. Things start at the other end. Something adds a KVO observer to your object using a particular key. The setter to hook into is determined from that key, but there's nothing preventing KVO from remembering the original key from which it derived the setter name. I kind of doubt that it reverse engineers the key from the method name.
> However NSOperation (for one) is documented to 'be KVO compliant for the property isFinished' and indeed you have to observe 'isFinished' if you want notifications.
True. I find this to be an awkward, non-standard aspect of NSOperation (especially since I once re-implemented my own version for Tiger compatibility). NSOperation does not have a "finished" property. It has an "isFinished" property.
Of course, it doesn't have any setter for that property, which avoids the weird setIsFinished: method name. It also means it's not a counter-example to your (correct) understanding that your setFoo: method would generate notifications for the "foo" key.
> If indeed the default KVO code generates notifications for 'foo' should you, if you have a boolean property using the 'isXXX' syntax, avoid default KVO and trigger your own property changes for isXXX?
No. There's no need to disable KVO's automatic notifications for this case, and you don't want the notifications to go out under key "isXXX".
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden