IOS 7 crash only [NSNull intValue]: unrecognized selector sent to instance

I want to get data from a JSON service. Only iOS 7 crashes when retrieving data from a JSON value. It returns from the JSON service below:

{ voteAverageRating = 0; voteCount = 0; } 

My code

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

His work for iOS 5,5,1,6,0,6.1, but it crashed only into the version of iOS7. He gave this error:

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

Then I changed my code below this;

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

When running this code. He crashed again only on iOS 7. He gave this error:

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

How can I solve this problem?

+8
json ios objective-c iphone ios7
source share
4 answers

Put a check before accessing a value from JSON, for example,

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

The reason for checking is that collection objects, such as NSDictionary , do not allow nil values, so they are stored as null. Passing intValue to NSNull will not work because it does not recognize this selector.

Hope this helps!

+35
source share

Like others, JSON null will be deserialized to NSNull . Unlike nil , you cannot send (most) messages to NSNull .

One solution is to add the -intValue implementation to NSNull through the category:

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

Your code will now work, since sending -intValue to NSNull will now return 0

Another option: you can also add the "IfNullThenNil" category to NSObject ...

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

Your code will now look like this:

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

Just add the -ifNullThenNil call wherever you access the values ​​from the JSON object.

+6
source share

It worked for me

 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"]]; } } 

Hope this helps someone. If you need any help let me know.

+1
source share

It's quite normal. JSON can send null values ​​to your application. If so, then this is done intentionally by the server, and it expects you to handle this. Find out what the correct behavior is when getting a null value. Then when you get the object, which may be a null value, check

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

The real problem is not that your application crashes, but that your application does not process the JSON data that it should process.

0
source share

All Articles