Only iOS 7 crash : unrecognized selector sent to instance

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;

My code

  • Screenshot in swift iOS?
  • Xcode iphone orientation support for certain views
  • NSDateFormatter, am I doing something wrong or is this a bug?
  • iOS memory usage increasing, can't find the culprit
  • Sort Array of Dictionaries by Key Value
  • Location permission alert on iPhone with Cordova
  • int voteCount = [listDic objectForKey:@"voteCount"] intValue] ;
    _LB_voteNumber.text = [NSString stringWithFormat:@"(%i)",voteCount];

    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]: unrecognized selector
    sent to instance

    Then i changed my code below that;

    NSString *voteCount = [listDic objectForKey:@"voteCount"] ;
    _LB_voteNumber.text = [NSString stringWithFormat:@"(%@)",voteCount];

    When runs this code. It crashed again only iOS 7 version. It gave this error:

    0x00098117 _mh_execute_header [NSNull length]: unrecognized selector
    sent to instance

    How can i solve this problem ?

    4 Solutions Collect From Internet About “Only iOS 7 crash : unrecognized selector sent to instance”

    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!

    As the others have said, JSON null will be deserialized to NSNull. Unlike nil, You cannot send (most) messages to NSNull.

    One solution is to add an implementation of -intValue on NSNull via category:

    @implementation NSNull (IntValue)
    -(int)intValue { return 0 ; }

    Now your code will work since sending -intValue to NSNull will now return 0

    Another option: You could also add an “IfNullThenNil” category to NSObject…

    @implementation NSObject (IfNullThenNil)
    -(id)ifNullThenNil { return self ; }
    @implementation NSNull (IfNullThenNil)
    -(id)ifNullThenNil { return nil ; }

    Now, your code becomes:

    int voteCount = [[[listDic objectForKey:@"voteCount"] ifNullThenNil] intValue] ;

    Just add a call to -ifNullThenNil wherever you access values from a JSON object.

    For me this is worked

    NSArray* merStore = [tmpDictn objectForKey:@"merchantStore"];
    if ([merStore isKindOfClass:[NSArray class]] && merStore.count !=0)
        for(int n = 0; n < merStore.count; n++)
            NSMutableDictionary *storeDic = [merStore objectAtIndex:n];
            [latitudeArray addObject:[storeDic objectForKey:@"latitude"]];

    I hope it helps some one. If you need any help let me know.

    That’s quite normal. JSON can send null values to your app. If it does, then this is done intentionally by the server and it expects you to handle it. Figure out what the correct behaviour is when a null value is received. Then when you get an object that could be a null value, check

    if (object == [NSNull null])
        // stuff to handle null objects
        // stuff to handle non-null objects

    The real problem isn’t that your app crashes, but that your app doesn’t handle JSON data that it is supposed to handle.