Mel
Mel

Reputation: 2075

NSMutableDictionary key values

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.

Upvotes: 4

Views: 2047

Answers (3)

umerh
umerh

Reputation: 160

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.

Upvotes: 0

Tom Dalling
Tom Dalling

Reputation: 24115

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

Upvotes: 3

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

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?

Upvotes: 6

Related Questions