ARC 不允许将 iOS 隐式转换为 NSNumber
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10651331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Implicit conversion of int to NSNumber is disallowed with ARC
提问by Daniel
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
问候, 丹尼尔
回答by rishi
Erro because you are comparing NSNumber
with int
.
错误,因为您正在NSNumber
与int
.
Try like -
尝试像 -
if ([success isEqual:[NSNumber numberWithInt:1]])
or
或者
if ([success intValue] == 1)
回答by rishi
You should use [success intValue] == 1
. An NSNumber is a class, so number is a pointer, not the direct value.
你应该使用[success intValue] == 1
. NSNumber 是一个类,所以 number 是一个指针,而不是直接值。
回答by omz
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:
NSNumber
是一个对象(即指针),因此您不能将它与像1
. 相反,您必须int
从数字对象中提取值:
if ([success intValue] == 1) {
...
}
回答by tilo
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!
}