1

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!

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126

4 Answers4

4

Since dictionaries contain Objective-C objects, an entry containing a number is an NSNumber instance. NSNumber provides a convenience method, -intValue, for extracting its underlying int value:

if ([[jsonDictionary objectForKey:@"Success"] intValue] == 1) { … }

Note that NSNumber has 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 in NSDictionary. The latter is declared in NSObject and 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.

  • you've saved me once again, thanks! The conditional works and now I gotta figure out how to act on. Objective-C has been a *slow* learning experience for me... – Gup3rSuR4c Jan 28 '11 at 21:44
  • @Alex Cheers. Be warned that `-intValue` will return `0` if the key is not in the dictionary, though. In some cases I first grab the object from the dictionary (e.g. `NSNumber *success = [jsonDictionary objectForKey:@"Success"];`) and test if it’s different from `nil` to make sure the key was available in the dictionary in the first place. Good luck with your project! –  Jan 28 '11 at 21:54
2

The number 1 is not an object pointer. Use an NSNumber instance instead if you want to use a number in an NSDictionary.

[[jsonDictionary objectForKey:@"Success"]
 isEqualToNumber:[NSNumber numberWithInteger:1]]
Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94
1
 [[jsonDictionary objectForKey:@"Success"] isEqualToNumber: [NSNumber numberWithInt:1]]
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

You can get the value of dictionary in different ways like checking the value first.

Solution 1: Using simple if statement.
int value = 0;
if ([[jsonDictionary objectForKey:@"Success"]intValue]==1){
    value = [[jsonDictionary objectForKey:@"Success"]intValue];
}

Solution 2: Using ternary operator
value = ([[jsonDictionary objectForKey:@"Success"]intValue]==1) ? 1:0;
handiansom
  • 783
  • 11
  • 27