Those of you who have studied C know that C is a zero for a nonexistent original value. NULL, as a NULL pointer, is also equivalent to 0 in the pointer environment. Actually NULL and 0 are the same value. But for purposes and purposes and easily identifiable reasons, NULL is used for Pointers and objects, and 0 is used for values.

NULL

To understand NULL, you need to know the following concepts:

  1. What is a null pointer constant?

    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

    An integer constant expression of type 0 or an expression of type void* is called a null-pointer constant.

  2. What is a null pointer?

    If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

    When a null pointer constant is converted to a pointer type. If the pointer does not point to any actual object or function, the pointer is called null.

    For example: char *p = 0; P is a null pointer. It doesn’t point to any real object. The reverse is true: the addresses of any actual objects and functions cannot be null Pointers.

  3. Where in memory does the null pointer point (the internal implementation of the null pointer)?

    The standard does not specify where in memory a null pointer points to, meaning that the specific address value (0x0 or a specific address) used to represent a null pointer depends on the implementation. A null pointer is a pointer that points to an address 0. At OC, the address to which the hollow pointer points is 0x0. The test code is as follows:

     char *p = 0;
     printf("%p\n", p); // 0x0Copy the code

With the above concepts in mind, let’s take a look at the definition of NULL

The following definition can be found in the header file stddef.h:

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

From the definition and the concepts above, NULL is a NULL pointer to the address 0x0. That is, this pointer does not point to any object or function.

nil

With NULL out of the way, let’s look at nil, which is objective-C added to the non-existence of C. Nil is a pointer to an object that doesn’t exist. Equivalent to NULL, they are semantically different but technically equivalent. The contents of the NSObject that has just been allocated memory are all set to 0, that is, null Pointers. Nil has a special behavior that even though it’s zero, it can still send it a message. Methods called on nil all return a zero value. The test code is as follows:

   NSString *name;
   NSLog(@"%p  %hhd", name, [name isEqualToString:@"CoderKo1o"]); // 0x0 0Copy the code

Nil

You can see this definition in apple’s official documentation

#define nil __DARWIN_NULL
#define Nil __DARWIN_NULLCopy the code

Nil and Nil have the same value, so what’s the difference between them?

nil

Defines the id of a null instance.

Nil

Defines the id of a null class.Copy the code

Nil is a pointer to an object that points to a value of 0. Nil is a pointer to a value of 0.

NSNull

According to the above statement, everything has a reason to exist, and NSNull has a reason to exist. NSNull is widely used in Foundation and other frameworks. Anyone familiar with collections like NSArray and NSDictionary knows that they have a nil value flaw because the syntax dictates that these collections end with a nil flag. So when you want to store an object with a null value. You can’t use nil (syntactically), which is where NSNull comes in. NSNull can be understood as effectively encapsulating NULL or nil values into an object. NSNull has only one class method + (NSNull *)null; Return a single object representing the value of 0. Note that this object does not point to the address 0x0.

conclusion

All the values introduced here are used to express no in OC, which can not be ignored in the process of learning OC. In order to show the difference of the four values more intuitively, a table is listed here:

mark value meaning
NULL (void *)0 Null pointer in C (value 0)
nil (id)0 Null pointer to an Objective-C object
Nil (Class)0 Null pointer to objective-C class
NSNull [NSNull null] A separate object representing a value of zero (not a null pointer)