views:

456

answers:

3

is it true that the key values for NSMutableDictionary can only be strings?

I was trying to use objects, but I am getting a warning.

+4  A: 

From the docs:

In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Key-Value Coding Fundamentals).

What warning are you getting?

Joshua Nozzi
as it turned out my class did not implement the protocal
Mel
yeah - definitely make sure you implement the "hash" method in your object. Otherwise, you can get two different objects appearing identical in the dictionary.
Ben Gotow
+2  A: 

You can use any object, but the object must implement -[NSObject hash], -[NSObject isEqual:], and the NSCopying protocol.

Tom Dalling
well all objects that inherit from NSObject implement `-hash`. it is only if you override `-isEqual:` that you have to make sure the `-hash` is compatible with `-isEqual:`
newacct
A: 

If you take a look at header file of NSMutableDictionary, the add function can take id as the key:

- (void)setObject:(id)anObject forKey:(id)aKey;
- (void)removeObjectForKey:(id)aKey;

So you can use virtually anything as the key and value.

umerh