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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:06:44  来源:igfitidea点击:

"Incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSNumber *'"

objective-cios

提问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 an int. You can only store objects in NSDictionary, 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 NSNumberor an NSStringinstance.

应该工作假设[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 intparameter, trying to convert an idto an intis 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 NSNumberobject, 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];