Reprinted from: blog.csdn.net/wzzvictory/…

Author: wangzz


Nil, nil, NULL, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil. Nil, nil, NULL, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull, NSNull

A, NULL,

  1. Position statement

Stddef. H file

  1. define
#undef NULL
#ifdef __cplusplus
#  if! defined(__MINGW32__) && ! defined(_MSC_VER)
#    define NULL __null
#  else
#    define NULL 0
#  endif
#else
#  define NULL ((void*)0)
#endif
Copy the code

__cplusplus indicates C++ code, so for the average iOS developer, the definition of NULL is: # define NULL ((void*)0). Therefore, NULL is essentially :(void*)0

  1. Usage and Meaning

NULL indicates that the C pointer is NULL

  1. The sample
char *string = NULL;
Copy the code

Second, the nil

  1. Position statement

Objc. H file

  1. define
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
Copy the code

Where __has_feature(cxx_nullptr) is used to determine whether nullptr is present in C++. For ordinary iOS developers, nil is defined as: #define nil __DARWIN_NULL means that nil is ultimately a macro definition of __DARWIN_NULL. __DARWIN_NULL is a macro defined in **_types.h**.

#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 */
Copy the code

The final definition of __DARWIN_NULL for non-C ++ code looks like this:

#define __DARWIN_NULL ((void *)0)
Copy the code

In other words, nil is essentially :(void *)0

  1. Usage and Meaning

The pointer used to indicate an object in Objective-C is null

  1. The sample
NSString *string = nil;
id anyObject = nil;
Copy the code

Third, Nil

  1. Position statement

Objc. H file

  1. define
#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif
Copy the code

Just like nil, nil is essentially :(void *)0

  1. Usage and Meaning

The value of the variable used to represent the Objective-C Class type is null

  1. The sample
Class anyClass = Nil;
Copy the code

Fourth, the NSNull

  1. Position statement

NSNull. H file

  1. define
@interface NSNull : NSObject <NSCopying.NSSecureCoding>
 
+ (NSNull *)null;
 
@end
Copy the code
  1. Usage and Meaning

As you can see by definition, NSNull is an Objective-C class, except that this class is quite special because it represents null values, meaning nothing. It also has only one singleton method +[NSUll null]. This class is typically used to hold an empty placeholder object in a collection object.

  1. The sample

We usually initialize the NSArray object as follows:

NSArray *arr = [NSArray arrayWithObjects:@"wang".@"zz".nil];
Copy the code

So when NSArray hits nil, that means that the elements of this array object are off, that NSArray only looks at anything before nil, anything after nil gets thrown away. For example:

NSArray *arr = [NSArray arrayWithObjects:@"wang".@"zz".nil.@"foogry"];
Copy the code

NSArray stores only wang and zz, and foogry is discarded. In this case, NSNull can be used:

NSArray *arr = [NSArray arrayWithObjects:@"wang".@"zz"[NSNull null],@"foogry"];
Copy the code

Five, the summary

As you can see from the previous introduction, whether NULL, nil, or nil, they’re essentially the same thing, they’re (void *)0, they’re just written differently. And the whole point of doing that is to distinguish between different data types, so when you see NULL you know it’s a C pointer, when you see nil you know it’s an Objective-C object, when you see nil you know it’s a Class type.