Re: mutable keys in NSMutableDictionary
Re: mutable keys in NSMutableDictionary
- Subject: Re: mutable keys in NSMutableDictionary
- From: Ali Ozer <email@hidden>
- Date: Tue, 7 Oct 2003 09:11:15 -0700
The problem is that -- as I understand from the documentation --
NSMutableDictionary creates its own copies of the keys internally (one
has to provide a copyWithZone function for that which I have written).
Therefore, it seems that after an insertion of a key-value pair, one
can never get the actual internal key objects. I do, however, need
these in the process of dynamically building the graph stored in the
NSMutableDictionary. Otherwise, it appears I will have essentially
duplicates of every key which will amount to a lot of memory.
The values of the keys cannot change once entered in a dictionary ---
this would corrupt the dictionary data structures. So the keys are
copied. If your keys are immutable (for instance an NSNumber, or an
immutable NSString), [string copy] will often reduce to a [string
retain], so the copy will be fast and memory efficient.
If your vertex objects are immutable, then feel free to override
copyWithZone: to call retain. However, if the vertex objects are not
immutable, and they might change behind the dictionaries back, this
could have bad consequences. In addition, it's "not right."
Alternatives are indeed to use CFDictionary or NSMapTable and supply
your own callbacks for managing the keys, and making sure the keys
don't change inappropriately. CFDictionary and NSMapTable are both
functional APIs and a little messier to use than NSDictionary, but they
aren't too bad. In both cases make use of a built-in callback structure
(for instance NSObjectMapKeyCallBacks) and if needed just modify the
function pointers you want to change.
Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.