NULL
对于学习过 C/C++ 语言的朋友,对 NULL 一定很熟悉吧?这就是在 C/C++ 中的空指针。
在 C 语言中, NULL 是无类型的,只是一个宏,它代表空。我们不研究 C++ 中的 NULL ,因为在 C++11 以后又有了新的定义,我们不深究。
这就是 C 语言中所谓的 NULL ( C++ 的定义比较复杂,这里不说了):
Objective-C
#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
这是在 stddef.h 头文件中声明的。这是使用了条件编译的, __cplusplus 这个宏表示 C++ ,对于我们 Objective-C 开发来说, NULL 就表示 ((void*)0)
像 C 语言中,我们定义了一个指针,当我们使用完以后,通常会设置指向 NULL 。如果没有设置,这个指针就成了所谓的野指针,然后其它地方不小心访问了这个指针是很容易造成非法访问的,常见的表现就是崩溃了。
既然 Objective-C 是基于 C 语言的面向对象语言,那么也会使用到 C 语言类型的指针,比如使用 const char * 类型,判断是否为空时,是使用 p != NULL 来判断的。
nil
对于我们学习 Objective-C 的人来说,这个是非常熟悉的。如下为官方定义:
Objective-C
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
对于我们 Objective-C 开发来说, nil 就是 __DARWIN_NULL 。看下官方定义:
Objective-C
#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 */
这个也是条件编译的,那么对于我们 Objective-C 开发来说, nil 就代表 ((void *)0) 。
我们使用 nil 表示 Objective-C 对象为空,如 NSString *str = nil 。
Nil
先看看官方是如何声明的:
Objective-C
#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif
根据条件,我们做 Objective-C 开发的,那么 Nil 也就是代表 __DARWIN_NULL ,而对于 __DARWIN_NULL 的声明如下:
Objective-C
#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 */










