使用ARC不允许将int隐式转换为NSNumber

Dan*_*iel 12 objective-c nsnumber ios

在下面的代码我得到错误消息:ARC不允许将'int'隐式转换为'NSNumber*'.

我错了什么?

<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>
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助或提示!

亲爱的,丹尼尔

ris*_*shi 27

Erro,因为你正在NSNumber与之比较int.

试试 -

if ([success isEqual:[NSNumber numberWithInt:1]])
Run Code Online (Sandbox Code Playgroud)

要么

if ([success intValue] == 1)
Run Code Online (Sandbox Code Playgroud)


小智 14

你应该用[success intValue] == 1.NSNumber是一个类,因此number是指针,而不是直接值.