平时开发过程中经常遇到这几个表示空的关键字:nil、Nil、NULL,一向搞不清楚,作为一个有两年开发经验的程序员,连那么基础的东西都不知道未免太丢人了。
首先要说明的是,nil、Nil、NULL三个关键字和NSNull类都是表示空,只是用处不一样,具体的区别如下:
一、NULL
1、声明位置
stddef.h文件
2、定义
#undef NULL
#ifdef __cplusplus
# if !defined(__MINGW32__) && !defined(_MSC_VER)
# define NULL __null
# else
# define NULL 0
# endif
#else
# define NULL ((void*)0)
#endif
其中__cplusplus表示是不是C++代码,所以对于普通的iOS开发者来说,通常NULL的定义就是:
<span style="font-weight: normal;"># define NULL ((void*)0) </span>
<span style="font-weight: normal;"># define NULL ((void*)0) </span>
因此,NULL本质上是:(void*)0
3、用处及含义
NULL表示C指针中的空值4、示例
<span style="font-weight: normal;">char *string = NULL; </span>
二、nil
1、声明位置
objc.h文件
2、定义
<span style="font-weight: normal;"><span style="font-size:14px;">#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif</span></span>
其中__has_feature(cxx_nullptr)用于判断C++中是否有nullptr特性,对于普通iOS开发者来说,nil的定义形式为:
<span style="font-weight: normal;"># define nil __DARWIN_NULL </span>
就是说nil最终是__DARWIN_NULL的宏定义,__DARWIN_NULL是定义在_types.h中的宏,其定义形式如下:
<span style="font-weight: normal;"># define nil __DARWIN_NULL </span>
<span style="font-weight: normal;">#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 */</span>
非C++代码的__DARWIN_NULL最终定义形式如下:
<span style="font-weight: normal;">#define __DARWIN_NULL ((void *)0) </span>
也就是说,nil本质上是:(void *)0
<span style="font-weight: normal;">#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 */</span>
<span style="font-weight: normal;">#define __DARWIN_NULL ((void *)0) </span>
3、用处及含义
用于表示指向Objective-C中对象的指针的值为空4、示例
<span style="font-weight: normal;">NSString *string = nil;
id anyObject = nil;</span>
三、Nil
1、声明位置
objc.h文件2、定义
<span style="font-weight: normal;">#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif</span>
和上面讲到的nil一样,Nil本质上也是:(void
*)03、用处及含义
用于表示Objective-C类(Class)类型的变量值为空4、示例
<span style="font-weight: normal;">Class anyClass = Nil; </span>
四、NSNull
1、声明位置
NSNull.h文件2、定义
<span style="font-weight: normal;">@interface NSNull : NSObject <NSCopying, NSSecureCoding>
+ (NSNull *)null;
@end</span>
3、用处及含义
从定义中可以看出,NSNull是一个Objective-C类,只不过这个类相当特殊,因为它表示的是空值,即什么都不存。它也只有一个单例方法+[NSUll null]。该类通常用于在集合对象中保存一个空的占位对象。4、示例
我们通常初始化NSArray对象的形式如下:<span style="font-weight: normal;">NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",nil]; </span>
当NSArray里遇到nil时,就说明这个数组对象的元素截止了,即NSArray只关注nil之前的对象,nil之后的对象会被抛弃。比如下面的写法<span style="font-weight: normal;">NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",nil,@"foogry"]; </span>
这是NSArray中只会保存wang和zz两个字符串,foogry字符串会被抛弃。
这种情况,就可以使用NSNull实现:
<span style="font-weight: normal;">NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",[NSNull null],@"foogry"]; </span>