Re: NSDictionary allValues not mutable
Re: NSDictionary allValues not mutable
- Subject: Re: NSDictionary allValues not mutable
- From: Greg Guerin <email@hidden>
- Date: Sun, 10 Oct 2010 13:44:30 -0700
Trygve Inda wrote:
Ultimately I have a masterDict containing a few thousand dicts (all
having
the same structure) which I need to convert to an array of dicts to be
displayed in an NSTable.
When I add to this array (quite rarely), I will actually add a new
dict to
the masterDict and then append this same object to the array
(managed by an
NSArrayController.
One of the items in each dict is the key that is used to place it
in the
masterDict so when it is in the array I still know the key that
refers to it
in the masterDict.
The reason for storing them as dicts in the masterDict instead of
just an
array is that I really need to be able to find them on a key-by-key
basis.... The array is only for the ability to put them in an NSTable
w/NSArrayController.
This seems odd to me, but it's difficult to be sure without details
on what the data is and how it's used.
If the primary organization is of a numbered tabular sequence, as it
seems to be, then the primary collection should be a numbered tabular
sequence (NSMutableArray because it's mutable). Other forms, like B-
trees, skip-lists, etc. would also be candidates, but those are still
fundamentally sequences, with a design that improves searching,
insertion, etc.
Secondarily, it seems you want to be able to search the sequence
quickly, for which you're using an NSDictionary. This doesn't
represent the order of the items, it's only an aid to searching. If
the dictionary is only used for speeding up searches, then the
primary organization is still sequential, and the dictionary is just
a speed-enhancer. Again, sorted trees or skip-lists would also speed
up searches, while presenting a fundamentally sequential nature.
So if the master dictionary and the array for the NSTable are
encapsulated together in a class, you could use NSMutableArray as the
primary structure, and encapsulate a hidden NSMutableDictionary used
only for faster searching. Both are maintained in parallel by the
enclosing class, so there's never any need to ask the dictionary for
its array of values, nor to copy an immutable array to make a mutable
version for table insertion. In short, make a class that
encapsulates all the desired behavior by itself, rather than using a
naked dictionary and the supplementary arrays it makes.
It's possible that such an encapsulating class could subclass
NSMutableArray, and maintain an internal dictionary only for fast
searching. Then that class could be used directly by
NSArrayController, while still performing fast searches.
Or maybe the whole thing can be redesigned using NSMutableSet, which
guarantees uniqueness and is intended for fast searching. It's
unordered, so might be unsuitable for use in a table, though it does
have the ability to produce a sorted array. See NSSet and NSMutableSet.
-- GG
_______________________________________________
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