On Jun 25, 2009, at 07:31, Rick Hoge wrote:

I recently tried a design where a certain object had an NSMutableDictionary instance variable, which in turn contained an NSMutableArray entry. After initializing this setup (in -init) as shown here:

topLevelDictionary = [NSMutableDictionary dictionary]; // An instance variable - I'm using GC [topLevelDictionary setValue:[NSMutableArray array] forKey:@"subArray"];

I then proceeded to add an NSTableView and NSArrayController to the nib file, binding the controller's content array to the keypath "topLevelDictionary.subArray"

...

All worked fine, except that, when the new dictionaries are added by clicking the add: button, the mutable array managed by the controller is replaced with a new array (rather than inserting the new dictionary in the original array). The new array has the correct content, apparently having copied all the pre-existing dictionaries from the old array.

This is correct. NSMutableDictionary implements 'setValue:forKey:' (as a convenience, to provide very basic KVC compliance) but it doesn't implement any indexed accessors, so the array is updated via this 'setValue:forKey:', which means that the array is completely replaced.

If you look at:

http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/SearchImplementation.html#/ /apple_ref/doc/uid/20000955-SW2

you'll see that case (1) doesn't apply, but case (2) does.

I confirmed that if subArray is promoted to an instance variable, then add: will perform an insertion rather than creating a new array (in fact I've used this a lot in the past). It seems that having the array be part of a mutable compound object results in creation of a new, copied array instead of an insertion.

Presumably you haven't overridden 'accessInstanceVariablesDirectly:' to return NO in your "certain object", so now case (3) applies, and that results in the array being updated instead of replaced.

The problem you've got is that you want "subArray" to behave like a fully KVO compliant indexed property, but you haven't implemented it as such.


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to