12

on following code i'm get the errormessage: Implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC.

What i'm making wrong?

<pre>
 <code>
  NSDictionary *results = [jsonstring JSONValue];
  NSNumber *success = [results objectForKey:@"success"]; // possible values for "success": 0 or 1

   if (success == 1) { // ERROR implicit conversion of int to NSNumber disallowed with ARC    
   }
 </code>
</pre>

Thanks for any help or hint!

regards, Daniel

Nate Kohl
  • 35,264
  • 10
  • 43
  • 55
Daniel
  • 123
  • 1
  • 1
  • 5

4 Answers4

28

Erro because you are comparing NSNumber with int.

Try like -

if ([success isEqual:[NSNumber numberWithInt:1]])

or

if ([success intValue] == 1)
rishi
  • 11,779
  • 4
  • 40
  • 59
  • 1
    The first comparison is wrong, you're comparing pointer values of distinct objects. You'd want `[success isEqual:[NSNumber numberWithInt:1]]` or better yet, the second comparison. – DarkDust May 18 '12 at 11:06
15

You should use [success intValue] == 1. An NSNumber is a class, so number is a pointer, not the direct value.

3

NSNumber is an object (i.e. a pointer), so you can't just compare it to a integer literal like 1. Instead you have to extract the int value from the number object:

if ([success intValue] == 1) {
   ...
}
omz
  • 53,243
  • 5
  • 129
  • 141
1

If success should indicate a boolean, you may want to try this

NSDictionary *results = [jsonstring JSONValue];
NSNumber *success = [results objectForKey:@"success"]; 

if ([success boolValue]) { 
  // success!
}
tilo
  • 14,009
  • 6
  • 68
  • 85