Only iOS 7 crash [NSNull intValue]: unrecognized selector sent to instance

前端 未结 4 1239
情话喂你
情话喂你 2021-02-03 11:45

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 =         


        
4条回答
  •  庸人自扰
    2021-02-03 12:06

    Put a check before accessing the value from JSON like,

    if([NSNull null] != [listDic objectForKey:@"voteCount"]) {
        NSString *voteCount = [listDic objectForKey:@"voteCount"];
        /// .... 
    }
    

    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.

    Hope that helps!

提交回复
热议问题