ios “使用'NSNumber *'类型的表达式初始化'int'的整数转换指针不兼容”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13238223/
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
"Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSNumber *'"
提问by Phil
I suspect this is something really simple, but I'm trying to get an int that I have stored in a dictionary and the line I'm using is this..
我怀疑这真的很简单,但我试图获得一个我存储在字典中的 int,我正在使用的行是这个..
int mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
But it's giving me the error...
但它给了我错误...
Incompatible pointer to integer conversion sending 'id' to parameter of type 'int'
不兼容的整数转换指针将“id”发送到“int”类型的参数
and
和
Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSNumber *';
指向整数转换的不兼容指针,使用类型为“NSNumber *”的表达式初始化“int”;
If I try...
如果我尝试...
NSNumber *mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
I basically get the same error. I know I'm probably just using the wrong syntax, but I'm not sure how else to write it.
我基本上得到了同样的错误。我知道我可能只是使用了错误的语法,但我不确定如何编写它。
Thanks for any help.
谢谢你的帮助。
回答by WDUK
A couple of points:
几点:
[NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
Returns an
NSNumber*
, which you're trying to store in an int.NSNumber* mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
won't work either, because
[templateObject valueForKey:@"mapX"]
isn't anint
. You can only store objects inNSDictionary
, not primitive types.
[NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
返回一个
NSNumber*
,您试图将其存储在 int 中。NSNumber* mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
也不会工作,因为
[templateObject valueForKey:@"mapX"]
不是int
. 您只能在 中存储对象NSDictionary
,而不能在原始类型中存储。
You might want to try (Assuming I have the types correct)
您可能想尝试(假设我的类型正确)
NSNumber* mapXNum = [templateObject valueForKey:@"mapX"];
int mapX = [mapXNum intValue];
回答by jungledev
int mapX = (int)[[templateObject valueForKey:@"mapX"] integerValue];
the above solution is assuming the templateObject valueForKey:@"mapX" returns an NSNumber. If it doesn't and you want to convert it to an NSNumber, use this solution instead:
上述解决方案假设 templateObject valueForKey:@"mapX" 返回一个 NSNumber。如果不是,并且您想将其转换为 NSNumber,请改用此解决方案:
int mapX = (int)[(NSNumber *)[templateObject valueForKey:@"mapX"] integerValue];
Typecasting in ObjC is the.worst.
ObjC 中的类型转换是最糟糕的。
回答by jungledev
int mapX = [NSNumber numberWithInt:[templateObject valueForKey:@"mapX"]];
makes no sense for two reasons (think about it for a moment), however
没有意义有两个原因(想一想),但是
NSNumber *mapX = [NSNumber numberWithInt:[[templateObject valueForKey:@"mapX"] intValue]];
should work assuming [templateObject valueForKey:@"mapX"]
is either an NSNumber
or an NSString
instance.
应该工作假设[templateObject valueForKey:@"mapX"]
是一个NSNumber
或一个NSString
实例。
回答by Adam Rosenfield
You have two problems. First, [templateObject valueForKey:@"mapX"]
returns an id
, which is a pointer to an Objective-C object, not an int
. And since the -[NSNumber numberWithInt:]
method takes an int
parameter, trying to convert an id
to an int
is an error.
你有两个问题。首先,[templateObject valueForKey:@"mapX"]
返回 an id
,它是一个指向 Objective-C 对象的指针,而不是一个int
。由于该-[NSNumber numberWithInt:]
方法接受一个int
参数,尝试将 an 转换id
为 anint
是一个错误。
The second problem is that you try to convert the result of [NSNumber numberWithInt:...]
to an int
, which is again the same problem: -[NSNumber numberWithInt:]
returns a pointer to an NSNumber
object, which is not an int
.
第二个问题是您尝试将 的结果转换[NSNumber numberWithInt:...]
为 an int
,这又是同样的问题:-[NSNumber numberWithInt:]
返回一个指向NSNumber
对象的指针,而不是int
。
Assuming that the value stored in your dictionary is in fact an NSNumber
, what you really want to do is this:
假设存储在您的字典中的值实际上是 an NSNumber
,您真正想要做的是:
NSNumber *mapXObj = [templateObject valueForKey:@"mapX"]; // Implicit cast from id to NSNumber*
int mapX = [mapXObj intValue];
Or more succinctly:
或者更简洁:
int mapX = [[templateObject valueForKey:@"mapX"] intValue];