Murali Krish Murali Krish - 3 months ago 15
iOS Question

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.

Answer

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];