so I’m having the most difficult of time pulling values out of an NSDictionary. Right now I just have a dictionary that is populated from a JSON call and it only contains a key named ‘Success’ with a value of 0 or 1.
How do I do a conditional on that value to check if its 0 or 1? I’ve tried a bunch of things, but I’m not getting anywhere. Here’s my current code:
[[jsonDictionary objectForKey:@"Success"] isEqualToNumber:1]
I’m getting passing argument 1 of 'isEqualToNumber:' makes pointer from integer without a cast' as a warning, and the app crashes when it hits that line anyway.
And a subquestion, what’s the difference between objectForKey and valueForKey? Which one should I use by default?
Anyway, this noob in Objective-C would truly appreciate some help on this. Thanks in advance!
Since dictionaries contain Objective-C objects, an entry containing a number is an
NSNumberinstance.NSNumberprovides a convenience method,-intValue, for extracting its underlyingintvalue:Note that
NSNumberhas other convenience methods for extracting its underlying value as other C data types.In most cases, you should use
-objectForKey:instead of-valueForKey:. The former is the canonical method to obtain an entry in the dictionary and is declared inNSDictionary. The latter is declared inNSObjectand is used in Key-Value Coding contexts, where the key must be a valid KVC key, and there’s additional processing — for instance, if you’re using-valueForKey:in a dictionary with a key that starts with @, that character is stripped from the key and[super valueForKey:key]is called.