我正在尝试使用代码解析NSDictionary中的整数
[activeItem setData_id:[[NSString stringWithFormat:@“%@”,[dict valueForKeyPath:@“data_id”]] integerValue]];@H_301_3@
但是,这给了我这个错误:不兼容的整数到指针转换将’NSInteger'(又名’int’)发送到’NSInteger *’类型的参数(又名’int *’)@H_301_3@
setData_id采用整数作为参数.如果我想解析一个字符串,[NSString stringWithFormat:@“%@”,[dict valueForKeyPath:@“data_id”]]完美无缺.@H_301_3@
我在这里做的是将valueForKeyPath的结果解析为String,然后从中解析一个整数.@H_301_3@
解决方法
你的setData_id:方法是如何声明的?
看起来它期望NSInteger *而不是NSInteger ……@H_301_3@
它被声明为:@H_301_3@
- ( void )setData_id: ( NSInteger )value;
否则,它意味着它被声明为:@H_301_3@
- ( void )setData_id: ( NSInteger * )value;
这可能是一个错字…如果你真的需要一个整数指针,那么你可以使用(假设你知道你在范围方面做了什么):@H_301_3@
NSInteger i = [ [ NSString stringWithFormat: @"%@",[ dict valueForKeyPath: @"data_id" ] ] integerValue ]; [ activeItem setData_id: &i ];
但我认为你刚刚输了一个错字,添加了一个指针(NSInteger *),而你的意思是NSInteger.@H_301_3@
注意:如果setData_id是属性,则同样适用:@H_301_3@
@property( readwrite,assign ) NSInteger data_id;
与:@H_301_3@
@property( readwrite,assign ) NSInteger * data_id;
我猜你写了第二个例子,虽然意思是第一个例子……@H_301_3@