[英]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,因为您正在将NSNumber
与int
进行比较。
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.