只有iOS 7崩溃[NSNull intValue]:无法识别的选择器发送到实例 [英] Only iOS 7 crash [NSNull intValue]: unrecognized selector sent to instance
问题描述
我想从JSON服务获取数据。从JSON值获取数据时,只有iOS 7版本崩溃。
它从以下JSON服务返回:
I want to get data from JSON service. Only iOS 7 version crash when get data from JSON value. It returns from JSON service below that:
{
voteAverageRating = 0;
voteCount = 0;
}
我的代码
int voteCount = [listDic objectForKey:@"voteCount"] intValue] ;
_LB_voteNumber.text = [NSString stringWithFormat:@"(%i)",voteCount];
它适用于iOS 5,5.1,6.0,6.1,但它仅崩溃iOS7版本。它给出了这个错误:
Its work for iOS 5,5.1,6.0,6.1 but it crash only iOS7 version. It gave this error:
0x00098117 _mh_execute_header [NSNull intValue]:无法识别的选择器
发送给实例
0x00098117 _mh_execute_header [NSNull intValue]: unrecognized selector sent to instance
然后我改变了我的代码;
Then i changed my code below that;
NSString *voteCount = [listDic objectForKey:@"voteCount"] ;
_LB_voteNumber.text = [NSString stringWithFormat:@"(%@)",voteCount];
运行此代码时。它再次崩溃只有iOS 7版本。它给出了这个错误:
When runs this code. It crashed again only iOS 7 version. It gave this error:
0x00098117 _mh_execute_header [NSNull length]:无法识别的选择器
发送给实例
0x00098117 _mh_execute_header [NSNull length]: unrecognized selector sent to instance
我该如何解决这个问题?
How can i solve this problem ?
推荐答案
在从JSON访问值之前先检查一下,
Put a check before accessing the value from JSON like,
if([NSNull null] != [listDic objectForKey:@"voteCount"]) {
NSString *voteCount = [listDic objectForKey:@"voteCount"];
/// ....
}
检查原因是,像 NSDictionary
这样的集合对象不允许值为 nil
,因此它们存储为null。将 intValue
传递给 NSNull
将无效,因为它无法识别此选择器。
Reason for checking is, collection objects like NSDictionary
do not allow values to be nil
, hence they are stored as null. Passing intValue
to a NSNull
will not work as it will not recognise this selector.
希望有所帮助!
这篇关于只有iOS 7崩溃[NSNull intValue]:无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!