我正在尝试使用代码解析NSDictionary中的整数
[activeItem setData_id:[[NSString stringWithFormat:@“%@”,[dict valueForKeyPath:@“data_id”]] integerValue]];
但是,这给了我这个错误:不兼容的整数到指针转换将’NSInteger'(又名’int’)发送到’NSInteger *’类型的参数(又名’int *’)
setData_id采用整数作为参数.如果我想解析一个字符串,[NSString stringWithFormat:@“%@”,[dict valueForKeyPath:@“data_id”]]完美无缺.
我在这里做的是将valueForKeyPath的结果解析为String,然后从中解析一个整数.
解决方法
你的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;
我猜你写了第二个例子,虽然意思是第一个例子……
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。