我想从 JSON 服务获取数据。仅 iOS 7 版本在从 JSON 值获取数据时崩溃。
它从下面的 JSON 服务返回:
{
voteAverageRating = 0;
voteCount = 0;
}
My code
int voteCount = [listDic objectForKey:@"voteCount"] intValue] ;
_LB_voteNumber.text = [NSString stringWithFormat:@"(%i)",voteCount];
它适用于 iOS 5、5.1、6.0、6.1,但仅在 iOS7 版本上崩溃。它给出了这个错误:
0x00098117 _mh_execute_header [NSNull intValue]:无法识别的选择器
发送到实例
然后我改变了下面的代码;
NSString *voteCount = [listDic objectForKey:@"voteCount"] ;
_LB_voteNumber.text = [NSString stringWithFormat:@"(%@)",voteCount];
当运行此代码时。仅 iOS 7 版本再次崩溃。它给出了这个错误:
0x00098117 _mh_execute_header [NSNull 长度]:无法识别的选择器
发送到实例
我怎么解决这个问题 ?
在访问 JSON 中的值之前进行检查,例如,
if([NSNull null] != [listDic objectForKey:@"voteCount"]) {
NSString *voteCount = [listDic objectForKey:@"voteCount"];
/// ....
}
检查的原因是,集合对象如NSDictionary
不允许值nil
,因此它们被存储为空。通过intValue
to a NSNull
将不起作用,因为它无法识别该选择器。
希望有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)