NSString *someString = nil; NSURL *someURL = nil; id someObject = nil; if (anotherObject == nil) // do something
// objc.h #ifndef nil # if __has_feature(cxx_nullptr) # define nil nullptr # else # define nil __DARWIN_NULL # endif #endif // __DARWIN_NULL in _types.h #define __DARWIN_NULL ((void *)0)
Class someClass = Nil; Class anotherClass = [NSString class];
// objc.h #ifndef Nil # if __has_feature(cxx_nullptr) # define Nil nullptr # else # define Nil __DARWIN_NULL # endif #endif
int *pointerToInt = NULL; char *pointerToChar = NULL; struct TreeNode *rootNode = NULL;
// in stddef.h #define NULL ((void*)0)
// 由於 nil 被用來用爲集合結束的標誌,因此 nil 不能存儲在 Foundation 集合裏。 NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil]; // 錯誤的使用 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:nil forKey:@"someKey"]; // 正確的使用 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSNull null] forKey:@"someKey"];
/* NSNull.h Copyright (c) 1994-2012, Apple Inc. All rights reserved. */ #import <Foundation/NSObject.h> @interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end
nil:指向一個對象的空指針spa
Nil:指向一個類的空指針指針
NULL:指向其餘類型(如:基本類型、C類型)的空指針對象
NSNull:一般表示集合中的空值blog