Nil, nil, NULL本质都差不多,三个基本上可以通用,定义大概就如下:
#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
#ifdef __cplusplus
#ifdef __GNUG__
#define __DARWIN_NULL __null
#else /* ! __GNUG__ */
#ifdef __LP64__
#define __DARWIN_NULL (0L)
#else /* !__LP64__ */
#define __DARWIN_NULL 0
#endif /* __LP64__ */
#endif /* __GNUG__ */
#else /* ! __cplusplus */
#define __DARWIN_NULL ((void *)0)
#endif /* __cplusplus */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
NSNull在苹果文档中是这样描述的:
The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).
也就是说,操作集合类数据类型对象时,需要使用NSNull来判断,而不是仅仅通过对象是否为Nil来判断。有些时候对象非空,但是值是空的(==[NSNull null])。
NSDictionary *dicValue = [NSDictionary dictionaryWithObjectsAndKeys:
@"A", @"B", [NSNull null], @"D", nil, @"E", Nil];
NSString *strValue = [dicValue objectForKey:@"B"];
if ( Nil != strValue )
{
if ( [NSNull null] != (NSNull*)strValue )
{
NSLog(@"strValue { %@ }",strValue);
}
else
{
NSLog(@"strValue NSNull null");
}
}
else
{
NSLog(@"strValue Nil");
}
strValue = [dicValue objectForKey:@"D"];
if ( Nil != strValue )
{
if ( [NSNull null] != (NSNull*)strValue )
{
NSLog(@"strValue { %@ }",strValue);
}
else
{
NSLog(@"strValue NSNull null");
}
}
else
{
NSLog(@"strValue Nil");
}
// 嘎嘎
// strValue = [dicValue objectForKey:@"E"];
strValue = [dicValue objectForKey:@"F"];
if ( Nil != strValue )
{
if ( [NSNull null] != (NSNull*)strValue )
{
NSLog(@"strValue { %@ }",strValue);
}
else
{
NSLog(@"strValue NSNull null");
}
}
else
{
NSLog(@"strValue Nil");
}