我正在尝试使用代码从 NSDictionary 中解析一个整数
[activeItem setData_id:[[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]] integerValue]];
但是,这给了我这个错误:Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'NSInteger *' (aka 'int *')
setData_id 接受一个整数作为参数。如果我想解析为字符串,[NSString stringWithFormat:@"%@", [dict valueForKeyPath:@"data_id"]]
可以完美运行。
我在这里所做的是将 valueForKeyPath 的结果解析为一个字符串,然后从中解析一个整数。
最佳答案
您的 setData_id:
方法是如何声明的?
看起来它需要一个 NSInteger *
而不是 NSInteger
...
要么声明为:
- ( void )setData_id: ( NSInteger )value;
您可以使用您的代码。
否则,表示声明为:
- ( void )setData_id: ( NSInteger * )value;
这可能是一个错字......如果你真的需要一个整数指针,那么你可以使用(假设你知道你在做什么在范围方面):
NSInteger i = [ [ NSString stringWithFormat: @"%@", [ dict valueForKeyPath: @"data_id" ] ] integerValue ];
[ activeItem setData_id: &i ];
但我认为您只是打错了字,添加了一个指针 (NSInteger *
),而您的意思是 NSInteger
。
注意:如果setData_id
是一个属性,同样适用:
@property( readwrite, assign ) NSInteger data_id;
对比:
@property( readwrite, assign ) NSInteger * data_id;
我猜你写了第二个例子,而第一个例子是......
关于ios - 不兼容的整数到指针的转换将 'NSInteger'(又名 'int')发送到类型为 'NSInteger *'(又名 'int *')的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001146/