ios 不兼容的整数到指针转换将“NSInteger”(又名“int”)发送到“NSInteger *”(又名“int *”)类型的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23001146/
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 Integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')
提问by JamEngulfer
I'm trying to parse an integer from a NSDictionary using the code
我正在尝试使用代码从 NSDictionary 解析一个整数
[activeItem setData_id:[[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]] integerValue]];
[activeItem setData_id:[[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]] integerValue]];
However, this is giving me this error: Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')
但是,这给了我这个错误: Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')
setData_id takes an integer as a parameter. If I want to parse to a string, [NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]
works perfectly.
setData_id 接受一个整数作为参数。如果我想解析为字符串,[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]
效果很好。
What I'm doing here is parsing the result of valueForKeyPath to a String, then parsing an integer from that.
我在这里做的是将 valueForKeyPath 的结果解析为一个字符串,然后从中解析一个整数。
回答by Macmade
How is your setData_id:
method declared?
你的setData_id:
方法是如何声明的?
Looks like it expect an NSInteger *
rather than a NSInteger
...
看起来它期望一个NSInteger *
而不是一个NSInteger
......
Either it is declared as:
要么声明为:
- ( void )setData_id: ( NSInteger )value;
And you can use your code.
你可以使用你的代码。
Otherwise, it means it is declared as:
否则,这意味着它被声明为:
- ( void )setData_id: ( NSInteger * )value;
It might be a typo... If you really need an integer pointer, then you may use (assuming you know what you are doing in terms of scope):
这可能是一个错字......如果你真的需要一个整数指针,那么你可以使用(假设你知道你在范围方面做什么):
NSInteger i = [ [ NSString stringWithFormat: @"%@", [ dict valueForKeyPath: @"data_id" ] ] integerValue ];
[ activeItem setData_id: &i ];
But I think you just made a typo, adding a pointer (NSInteger *
), while you meant NSInteger
.
但我认为您只是打了一个错字,添加了一个指针 ( NSInteger *
),而您的意思是NSInteger
.
Note: If setData_id
is a property, the same applies:
注意:如果setData_id
是属性,同样适用:
@property( readwrite, assign ) NSInteger data_id;
versus:
相对:
@property( readwrite, assign ) NSInteger * data_id;
I guess you wrote the second example, while meaning the first one...
我猜你写了第二个例子,而意思是第一个......
回答by Leo Natan
The property is defined incorrectly.
属性定义不正确。
It should be:
它应该是:
@property (readwrite) NSInteger data_id;
instead of
代替
@property (readwrite) NSInteger *data_id;
You are attempting to pass an integer value to a format that expects to have a pointer type.
您正在尝试将整数值传递给期望具有指针类型的格式。
Either use
要么使用
[activeItem setData_id:[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]];
or
或者
[activeItem setData_id:[NSString stringWithFormat:@"%d", [[dict valueForKeyPath:@"data_id"] integerValue]]];
If you need to set an integer, drop the [NSString stringWithFormat:@"%@"]
- this creates a string.
如果您需要设置一个整数,请删除[NSString stringWithFormat:@"%@"]
- 这将创建一个字符串。
[activeItem setData_id:[[dict valueForKeyPath:@"data_id"] integerValue]];
回答by zeeawan
Use integerValueand intValuewhere appropriate.
在适当的地方使用integerValue和intValue。