murali kurapati
murali kurapati

Reputation: 1586

How to get the value for key @int from NSDictionary?

I am Creating a NSDictionary and adding a key value pair as below

  NSDictionary* dictionary = @{ @0: @"I am the value" };

and retrieving the value as below

  NSString* value = [dictionary valueForKey:@0];

Application crashed for doing this, I don't understand the reason, I am giving the same data type and value.

What is the datatype of @0, I guess it is NSNumber, If not correct me.

Upvotes: 2

Views: 8068

Answers (2)

Florian Burel
Florian Burel

Reputation: 3456

valueForKey: is not the proper method for what you are trying to achieve.

you should use:

NSString* value = [dictionary objectForKey:@0];

ValueForKey is for key value coding and expect a NSString as parameters

Upvotes: 1

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

Yes, it's NSNumber type however the problem is somewhere else.

You should call objectForKey instead of valueForKey which is mainly used for KVO.

NSString *value = [dictionary objectForKey:@0];

or better:

NSString *value = dictionary[@0];

Upvotes: 7

Related Questions