• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSArrayController's add: swaps entire content array when array is accessed via keypath
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArrayController's add: swaps entire content array when array is accessed via keypath


  • Subject: Re: NSArrayController's add: swaps entire content array when array is accessed via keypath
  • From: Quincey Morris <email@hidden>
  • Date: Thu, 25 Jun 2009 10:35:53 -0700

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@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


  • Follow-Ups:
    • Re: NSArrayController's add: swaps entire content array when array is accessed via keypath
      • From: Rick Hoge <email@hidden>
References: 
 >NSArrayController's add: swaps entire content array when array is accessed via keypath (From: Rick Hoge <email@hidden>)

  • Prev by Date: Application Support Directory Questions
  • Next by Date: Re: IBPlugin embedding question
  • Previous by thread: Re: NSArrayController's add: swaps entire content array when array is accessed via keypath
  • Next by thread: Re: NSArrayController's add: swaps entire content array when array is accessed via keypath
  • Index(es):
    • Date
    • Thread