<a name="Chart">區(qū)別</a>
Symbol | Value | Meaning |
---|---|---|
NULL | (void *)0 | literal null value for C pointers |
nil | (id)0 | literal null value for Objective-C objects |
Nil | (Class)0 | literal null value for Objective-C classes |
NSNull | [NSNull null] | singleton object used to represent null |
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:基本數(shù)據(jù)對象指針為空
NULL是無類型的,只是一個宏,它代表為空。用于c語言的各種數(shù)據(jù)類型的指針為空。例如:
intint *pointerToInt = NULL;
charchar *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"]