简体   繁体   English

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

[英]iOS Implicit conversion of int to NSNumber is disallowed with ARC

on following code i'm get the errormessage: Implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC. 在下面的代码我得到错误消息:ARC不允许将'int'隐式转换为'NSNumber *'。

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 亲爱的,丹尼尔

Erro because you are comparing NSNumber with int . Erro,因为您正在将NSNumberint进行比较。

Try like - 试试 -

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

or 要么

if ([success intValue] == 1)

You should use [success intValue] == 1 . 你应该使用[success intValue] == 1 An NSNumber is a class, so number is a pointer, not the direct value. NSNumber是一个类,因此number是指针,而不是直接值。

NSNumber is an object (ie a pointer), so you can't just compare it to a integer literal like 1 . NSNumber是一个对象(即指针),所以你不能仅仅把它比作字面像一个整数1 Instead you have to extract the int value from the number object: 相反,您必须从数字对象中提取int值:

if ([success intValue] == 1) {
   ...
}

If success should indicate a boolean, you may want to try this 如果success应该指示一个布尔值,您可能想尝试这个

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

if ([success boolValue]) { 
  // success!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不允许将&#39;NSInteger&#39;(又名&#39;unsigned int&#39;)隐式转换为&#39;NSNumber *&#39; - Implicit conversion of 'NSInteger' (aka 'unsigned int') to 'NSNumber *' is disallowed with arc ARC不允许将&#39;int&#39;隐式转换为&#39;NSDictionary *&#39; - Implicit conversion of 'int' to 'NSDictionary *' is disallowed with ARC ARC不允许将&#39;int&#39;隐式转换为&#39;UILabel *&#39; - Implicit conversion of 'int' to 'UILabel *' is disallowed with ARC ARC不允许将&#39;int&#39;隐式转换为&#39;id&#39; - Implicit conversion of 'int' to 'id' is disallowed with ARC 尝试填充NSMutableArray但是使用arc不允许将int隐式转换为id - Trying to populate NSMutableArray but get implicit conversion of int to id is disallowed with arc Obj-C错误“ARC不允许将Int隐式转换为集合* - Obj-C Error "Implicit Conversion of Int to Collection * is disallowed with ARC 在目标C中进行比较 - ARC不允许将&#39;int&#39;隐式转换为&#39;id&#39; - Comparing in objective C - Implicit conversion of 'int' to 'id' is disallowed with ARC 从Objective-C指针到int *的隐式转换不允许使用ARC - Implicit Conversion from Objective-C Pointer to int * is Disallowed With ARC 在 Objective-C 中,arc 不允许将 int 隐式转换为 NSString - implicit conversion of int to NSString is disallowed with arc in Objective-C 隐式将NSNumber *转换为int - implicit conversion NSNumber* to int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM