Hermann Klecker
Hermann Klecker

Reputation: 14068

NSMutableDictionary setObjetct forKey id NSString

Frankly, it is rather a detail question. Apples documentation of NSMutableDictionary https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html states:

setObject:forKey:
Adds a given key-value pair to the dictionary.
- (void)setObject:(id)anObject forKey:(id)aKey

According to that the parameter forKey accepts any object. However, when I try to pass an NSNumber Incompatible pointer types sending 'NSNumber *' to parameter of type 'NSString *' Aparently some NSString only is accepted as key.

For the time beeing I will convert my number to a string. In the end it is just a key. But does anybody know who is right? The documentation or the compiler?

Upvotes: 2

Views: 1404

Answers (3)

CSolanaM
CSolanaM

Reputation: 3368

Apple's documentation is right, maybe you were confusing the method setObject forKey with the setValue forKey as @mipadi said.

Upvotes: 3

Edwin Iskandar
Edwin Iskandar

Reputation: 4119

this works fine:

 [someMutableDictionary setObject:@"a string" forKey:[NSNumber numberWithInt:1]];

what does your code look like?

Upvotes: 0

mipadi
mipadi

Reputation: 410932

You shouldn't get that warning when using setObject:forKey:. However, you will get that warning when using the similarly-named setValue:forKey:. The latter, while it appears similar in name, is part of the key-value coding system and thus only accepts an NSString as the key.

Here's a sample program to demonstrate the difference.

Upvotes: 11

Related Questions