iOS Implicit conversion of int to NSNumber is disallowed with ARC
Asked Answered
C

4

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

Communitarian answered 18/5, 2012 at 10:55 Comment(0)
G
15

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

Graticule answered 18/5, 2012 at 10:57 Comment(0)
L
28

Erro because you are comparing NSNumber with int.

Try like -

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

or

if ([success intValue] == 1)
Lip answered 18/5, 2012 at 10:57 Comment(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.Stopping
G
15

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

Graticule answered 18/5, 2012 at 10:57 Comment(0)
I
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) {
   ...
}
Infirmary answered 18/5, 2012 at 10:58 Comment(0)
S
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!
}
Shape answered 18/5, 2012 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.