views:

259

answers:

2

Hello,

I am using NSDictionary as an associated array (i.e, the keys i am using can be any arbitrary objects). One of the very annoying thing about NSDictionary is that it always make a copy of the key and store it. In my scenario, I will later retrieve the keys from the NSDictionary and do some operations with them. The operation happens to depend on the object identity of the keys. Because the keys i retrieved later are copies of the objects i originally used as keys. The later object identity check fails.

My question is, is there any hashtable-like data structure in the iPhone 3.0 SDK that doesn't make copy of the keys? Thank you.

Outdateboy

+2  A: 

If you don't want your key to be copied (or even retained), you can use CFDictionary and supply a kCFTypeDictionaryKeyCallbacks or NULL or customized key callbacks.

To check if objects are equal you should use -isEqual: instead of ==.

KennyTM
And if you're comparing objects of your own class, you just implement your own -isEqual method in the class.
Joe McMahon
Why use `NULL` callbacks? Just use `kCFTypeDictionaryKeyCallBacks` (retain keys) instead of `kCFCopyStringDictionaryKeyCallBacks` (copy keys).
Peter Hosey
@Peter: `kCFCopyStringDictionaryKeyCallBacks` is not used at all in `NSDictionary` so I guess the asker's concern is not just copying.
KennyTM
KennyTM: Huh? The only thing the questioner asked for was a dictionary that doesn't copy the keys. NSMutableDictionary, and CFMutableDictionary with `kCFCopyStringDictionaryKeyCallBacks`, copy the keys. CFMutableDictionary with `kCFTypeDictionaryKeyCallbacks` does not copy the keys.
Peter Hosey
@Peter: (1) You *can't* pass callbacks to `NSMutableDictionary`-s. It always `-retain`-s. (2) Probably the asker don't want the keys to be retained either, so I start the answer with "don't want your key to be *retained* or copied".
KennyTM
(1) I didn't say you could. Your suggestion was to create a CFDictionary. My suggestion was to use `kCFTypeDictionaryKeyCallbacks` when doing so. (Also, NSMutableDictionary always *copies*.) (2) I assume the opposite: The questioner does want the dictionary to retain the keys, like usual. He just doesn't want it to copy them, so that he can use compare their pointers elsewhere. (On that note, perhaps he should make a custom callback structure, if only so that the dictionary will also compare keys by pointer equality.)
Peter Hosey
@Peter: sorry you're right. It always copies. (I thought of retain because I saw code using `UITouch` as keys).
KennyTM
A: 

NSDictionary is toll-free bridged with CFDictionary. So just do:

CFDictionarySetValue((CFDictionaryRef)myMutableDict, key, object);

It's only the Cocoa method that copies the key.

Mike Abdullah
I wouldn't rely on NSMutableDictionaries-as-CFMutableDictionaries not using the copy-string callbacks. The documentation doesn't explicitly define (so far as I could find) what happens when you do that, but if anything, I would consider that a bug.
Peter Hosey
I guess it's hard to know; the docs aren't very clear. I'm going on a combination of "it's always been that way" and that the docs state that all *methods* for placing objects in a dictionary will copy the key.
Mike Abdullah