Null
For a friend who has learned C + + language, must be familiar with NULL? This is the null pointer in C/s + +.
In C, NULL is no type, just a macro, which represents the null. We don't study NULL in C + + because we have a new definition after c++11, we don't delve into it.
This is the so-called NULL in C language (c + + definition is more complex, here is not said):
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
This is declared in the stddef.h header file. This is compiled using conditional, __cplusplus this macro represents C + +, for our objective-c development, NULL means ((void*)0)
Like the C language, we define a pointer that, when we are done with it, usually sets the point to NULL. If there is no setting, the pointer becomes the so-called wild pointer, and then other places inadvertently access to this pointer is very easy to create illegal access, the common performance is to crash.
Since Objective-c is based on the C language object-oriented language, it will also use the C language type of pointers, such as the use of const char * types, to determine whether it is empty, is used p != NULL to determine.
Nil
For those of us who study objective-c, this is very familiar. The following are official definitions:
Objective-c
#ifndef Nil
# if __has_feature (cxx_nullptr)
# define Nil nullptr #
Else
# define nil __darwin_null
# endif
#endif
For our objective-c development, nil is __DARWIN_NULL . Look at the official definition:
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/* __CPLUSP LUs *
This is also conditional compilation, then for our objective-c development, nil on behalf ((void *)0) .
We use nil to indicate that the Objective-c object is empty, such as NSString *str = nil .
Nil
Let's see how the official statement is made:
Objective-c
#ifndef Nil
# if __has_feature (cxx_nullptr)
# define Nil nullptr #
Else
# define Nil __darwin_null
# endif
#endif
According to the conditions, we do objective-c development, then Nil is the representative __DARWIN_NULL , and __DARWIN_NULL the statement as follows:
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/* __CPLUSP LUs *
This is also conditional compilation, then for our objective-c development, Nil also represents ((void *)0) .
But it is used to represent the empty class. Like what:
Objective-c
Nsnull
First look at the official statement:
Objective-c
Ns_assume_nonnull_begin
@interfaceNSNull: NSObject <nscopying, nssecurecoding>
+ (nsnull *) null;
@end
Ns_assume_nonnull_end
From this we know that Nsnull is inherited from NSObject the type. It is a very special class, it means empty, nothing is stored, but it is an object, just a placeholder object.
The use of the scene is not the same, such as the server interface let us in the value is empty, the pass-empty.
Objective-c
Nsdictionry *parameters = @{@ "arg1": @ "value1",
@ "arg2": Arg2.isempty? [Nsnull null]: arg2};
This is just a handy example, of course, we can not pass this person's parameters. If we want to unify, for example runtime , by dynamically turning objects into our parameters, we can unify the values of nil to [NSNull null]
Difference
NULL, nil, nil these three are the same for the objective-c median, (void *)0 so why should we distinguish between them? And what's the difference between Nsnull and the other:
NULL is a macro , which is used for the C language pointer, which represents the null pointer
Nil is a macro that is used for an object in Objective-c, representing an empty object
Nil is a macro that is used for classes in Objective-c, which indicates that the class points to null
Nsnull is a class type that is used to represent an empty placeholder object, similar to that of JS or server-side null
Summarize
The above is only a small series of personal insights, if the question, welcome to comment on the discussion, thank you for your support cloud habitat community.