Transferred from: http://nshipster.cn/nil/
Understanding the concept of "non-existent" is not only a philosophical problem, but also a practical one. We are the inhabitants of the physical universe, and the reason lies in the uncertainty of the existence of the logical universe. As a physical embodiment of a logical system, the computer faces a thorny problem, that is, how to use the existence of expression does not exist .
In Objective-c, there are several different kinds of non-existent . The reason for this is to go back to a frequently mentioned nshipster, explaining how objective-c bridges in C's program paradigm and in object-oriented paradigms inspired by Smalltalk.
C 0
is used as a raw value that does not exist , and NULL
as a pointer (this is equivalent to in the pointer environment 0
).
Objective-c is added on the basis that the expression of C does not exist nil
. nil
is a pointer to an object that does not exist. Although it NULL
is semantically different, they are technically equal.
At the framework level, the foundation defines a NSNull
class method that +null
returns a separate NSNull
object. NSNull
as nil
well as NULL
different, because it is an actual object instead of a 0 value.
In addition, in foundation/nsobjcruntime.h, it Nil
is defined as a pointer to a class that is zero-oriented. This little- nil
known uppercase cousin doesn't often appear, but it's worth noting at least.
About
nil
Some of the things
The content that was just been 分配
NSObject
set to 0
. This means that all pointers to other objects from that object nil
start, so init
self.(association) = nil
it is not necessary to set such expressions in the method.
Perhaps the nil
most notable behavior is that although it is zero, it can still have messages sent to it.
In other languages, such as C + +, this will cause your program to crash, but in Objective-c, the nil
call method returns a value of 0. This greatly simplifies the expression because it avoids nil
checking it before use:
// 举个例子,这个表达...if (name != nil && [name isEqualToString:@"Steve"]) { ... }// …可以被简化为:if ([name isEqualToString:@"steve"]) { ... }
Knowing nil
how to work in Objective-c allows you to turn this convenience into a feature rather than a bug lurking in your app. Be sure to avoid nil
situations where values are not needed, either by checking or returning early to quiet failures, or by adding one NSParameterAssert
to throw an exception.
NSNull
: There is no
NSNull
is widely used in foundation and other frameworks to resolve defects such as NSArray
and NSDictionary
the like and such collections cannot have nil
values. You can NSNull
encapsulate boxing as valid values to NULL
achieve the purpose of nil
using them in the collection:
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]
In general, the four expressions here do not have values that every objective-c programmer should know:
NULL
(void *) 0
The literal 0 value of the C pointer
Nil
(ID) 0
The literal 0 value of the Objective-c object
Nil
(Class) 0
The literal 0 value of the Objective-c class
NSNull
[NSNull NULL]
A separate object used to represent a value of 0
Objective-c in Nil/nil/null/nsnull