1. nil 对象为空
NSObject* obj = nil;
if (nil == obj) {
NSLog(@"obj is nil");
} else {
NSLog(@"obj is not nil");
}
2. Nil 类为空
Class someClass = Nil;
Class anotherClass = [NSString class];
3. NULL 基本数据对象指针为空
int *pointerToInt = NULL;
char *pointerToChar = NULL;
struct TreeNode *rootNode = NULL;
4. NSNull:
集合对象无法包含nil
作为其具体值,如NSArray
、NSSet
和NSDictionary
。相应地,nil
值用一个特定的对象 NSNull
来表示。NSNull
提供了一个单一实例用于表示对象属性中的的nil
值。
@interface NSNull : NSObject <NSCopying, NSSecureCoding>
+ (NSNull *)null;
@end
在NSNull
单例类中,提供了唯一的方法null:Returns the singleton instance of NSNull
.
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`
NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]