IOS中判断JSON返回数据是否为NSNull

JSON返回字符串数据,会有数据为“<null>”的情况出现。

这时候如果判断长度或者赋值给其它的对象的话就出崩溃,console会出现“class-name NSNull ****”的字样。

因此,在获得数据进一步使用前要先判断是否为NSNull.

 

苹果官方文档是这样描述的:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/NumbersandValues/Articles/Null.html

 1 id aValue = [arrayWithNull objectAtIndex:0];
 2 if (aValue == nil) {
 3     NSLog(@"equals nil");
 4 }
 5 else if (aValue == [NSNull null]) {
 6     NSLog(@"equals NSNull instance");
 7     if ([aValue isEqual:nil]) {
 8         NSLog(@"isEqual:nil");
 9     }
10 }
11 // Output: "equals NSNull instance"

因此我们可以依照 if (aValue == [NSNull null]) 这样判断,problem solved.

 

posted @ 2015-10-14 13:11  Kimi_Lee  阅读(1521)  评论(0编辑  收藏  举报