Re: KVO question
Re: KVO question
- Subject: Re: KVO question
- From: Ken Thomases <email@hidden>
- Date: Wed, 18 Nov 2015 02:09:08 -0600
On Nov 17, 2015, at 7:18 PM, Graham Cox <email@hidden> wrote:
>
> I’m using KVO to observe a bunch of properties.
>
> Most of these properties are split out into simple things that set a single value, but internal to my object, some end up setting a common dictionary of attributes, which is also a property.
>
> I KVO observe both the simple properties and the common dictionary property. Mostly this is working, but when code directly sets the dictionary property, the other properties don’t trigger their observers, even though I’m implementing the +keyPathsForValuesAffecting<property>, which I believe should cause the additional triggering. Have I understood that right?
If by "code directly sets the dictionary property", you mean always in a KVO-compliant manner, then yes.
> @property (retain) id<NSObject> thingy;
> @property (retain) NSDicitonary* dictionaryOfThings;
It's not clear if this is a public or private property. If it's public, and probably even if it's private, this should be a "copy" property, not just "retain". If it's just "retain", you're not protected against somebody setting it to a mutable dictionary and then subsequently mutating the dictionary, thereby effectively mutating your property in a non-KVO-compliant manner.
All of that said, I agree with Quincey. You should just use either normal properties backed by (compiler-synthesized) instance variables, or you can use a secondary object with such properties. Using a dictionary may seem like a quick-and-dirty solution, but invites problems in the long run.
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