Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa's NSDictionary: why are keys copied?

Tags:

All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those objects are copied when they're used in the dictionary.

I frequently want to use heavier weight objects as keys, simply to map one object to another. What I really mean when I do that is effectively:

[dictionary setObject:someObject forKey:[NSValue valueWithPointer:keyObject]]; 

("When I come back and hand you this same key object instance again, get me that same value out.")

...which is exactly what I end up doing to get around this design sometimes. (Yes, I know about NSMapTable in desktop Cocoa; but e.g. iPhone doesn't support this.)

But what I don't really get is why copying the key is necessary or desirable in the first place. What does it buy the implementation or caller?

like image 469
Ben Zotto Avatar asked Mar 06 '10 21:03

Ben Zotto


People also ask

Does NSDictionary copy values?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values.

Does NSDictionary retain objects?

An NSDictionary will retain it's objects, and copy it's keys. Here are some effects this has had on code I've worked on. Sometimes you get the same object you put in, sometimes not. Immutable objects are optimized to return themselves as a copy .


1 Answers

The copy ensures that the values used as keys don't change "underhand" while being used as keys. Consider the example of a mutable string:

NSMutableString* key = ...  NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];  [dict setObject: ... forKey: key]; 

Let's assume that the dictionary did not copy the key, but instead just retained it. If now, at some later point, the original string is modified, then it is very likely that you are not going to find your stored value in the dictionary again even if you use the very same key object (i.e., the one key points to in the example above).

In order to protect yourself against such a mistake, the dictionary copies all keys.

Note, by the way, that it is simple enough to define -copyWithZone: as just doing return [self retain]. This is allowed and good code if your object is immutable, and the NSCopying contract is specifically designed such that the object returned has to be (sorta, kinda) immutable:

Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.

(from NSCopying Reference)

and

The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

(from -copyWithZone: Reference)

Even if your objects are not immutable, you might get away with that implementation if you only ever use identity-based equality/hash implementations, i.e., implementations which are not affected in any way by the object's internal state.

like image 79
Dirk Avatar answered Oct 20 '22 05:10

Dirk