Objective-C 中不同类型的“空值”
以 NSDictionary
的 objectForKey
方法为例,如果字典中不存在指定的 key,返回值为 nil
,如果指定的 key 存在,但是值为空,返回值则是 [NSNull null]
。
There are three possible interpretations of "null" NSString:
someStringPtr == nil
(id)someStringPtr == [NSNull null]
someStringPtr.length == 0
If you may have the possibility of all 3, the 3rd check subsumes the first, but I don't know of a simple check for all three.
In general, JSON will return [NSNull null] for a null JSON value, but some kits may return @"" (length == 0) instead. nil will never be used in iOS since it can't be placed in arrays/dictionaries.
被这个问题坑了很久。