#ifndef nil # if __has_feature(cxx_nullptr) # define nil nullptr # else # define nil __DARWIN_NULL # endif #endif在Objective-C中用于id类型的对象
NSString *name = nil; NSURL *url = nil; id object = nil;
#ifndef Nil # if __has_feature(cxx_nullptr) # define Nil nullptr # else # define Nil __DARWIN_NULL # endif #endif在Objective-C中用于Class类型的对象
Class aClass = Nil; Clsss bClass = [NSURL class];
#if defined(__need_NULL) #undef NULL #ifdef __cplusplus # if !defined(__MINGW32__) && !defined(_MSC_VER) # define NULL __null # else # define NULL 0 # endif #else # define NULL ((void*)0) #endif多用于如下例子:
int *pInt = NULL; char *chChar <span style="white-space:pre"> </span>= NULL; struct stStruct = NULL;
NSArray *array = [NSArray arrayWithObjects: [[NSObject alloc] init], [NSNull null], @"aaa", nil, [[NSObject alloc] init], [[NSObject alloc] init], nil]; NSLog(@"%ld", array.count); // 输出 3,NSArray以nil结尾
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @"Object0", @"Key0", @"Object1", @"Key1", nil, @"Key-nil" @"Object2", @"Key2", nil]; NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; [mutableDictionary setObject:nil forKey:@"Key-nil"]; // 会引起Crash [mutableDictionary setObject:[NSNull null] forKey:@"Key-nil"]; // 不会引起Crash
[mutableDictionary setObject:(nil == value ? [NSNull null] : value) forKey:@"Key"];
原文地址:http://blog.csdn.net/lixuwen521/article/details/45718331