In oC, nil, nil, null, and nsnull may often occur. The differences are as follows:
Symbol |
Value |
Meaning |
Null |
(Void *) 0 |
Literal null value for C pointers |
Nil |
(ID) 0 |
Literal null value for objective-C objects |
Nil |
(Class) 0 |
Literal null value for objective-C classes |
Nsnull |
[Nsnull null] |
Singleton object used to represent null |
1. Nil: the object is empty.
Defines an instance object as a null value. For example:
[Objc]View plaincopy
- Nsobject * OBJ = nil;
- If (nil = OBJ ){
- Nslog (@ "obj is nil ");
- }
- Else {
- Nslog (@ "obj is not nil ");
- }
Ii. Nil: the class is empty.
Define a class as null. For example:
[Objc]View plaincopy
- Class someclass = nil;
- Class anotherclass = [nsstring class];
Iii. null: the pointer to the basic data object is null.
Pointers for various data types in C language are null. For example:
[Objc]View plaincopy
- Intint * pointertoint = NULL;
- Charchar * pointertochar = NULL;
- Struct treenode * rootnode = NULL;
Iv. nsnull
A collection object cannot contain nil as its specific value, such as nsarray, nsset, and nsdictionary. Correspondingly, the nil value is represented by a specific object nsnull. Nsnull providesSingle InstanceIndicates the nil value in the object property.
[Objc]View plaincopy
- @ Interface nsnull: nsobject <nscopying, nssecurecoding>
- + (Nsnull *) NULL;
- @ End
The unique method NULL: returns the singleton instance of nsnull is provided in the nsnull Singleton class.
For example:
[Objc]View plaincopy
- Nsmutabledictionary * mutabledictionary = [nsmutabledictionary dictionary];
- Mutabledictionary [@ "somekey"] = [nsnull null]; // sets value of nsnull Singleton for 'somekey'
- Nslog (@ "keys: % @", [mutabledictionary allkeys]); // @ [@ "somekey"]
V. Description:
Technically they're all the same, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.
If someone sees you passing null, they know the specified er expects a C pointer. if they see nil, they know the specified er is expecting an object. if they see nil, they know the handler er is expecting a class. readability.
Vi. Note
Here are a few interesting examples:
(1)
[Objc]View plaincopy
- Nsobject * obj1 = [[nsobject alloc] init];
- Nsobject * obj2 = [nsnull null];
- Nsobject * obj3 = [nsobject new];
- Nsobject * obj4;
- Nsarray * arr1 = [nsarray arraywithobjects: obj1, obj2, obj3, obj4, nil];
- Nslog (@ "arr1 count: % lD", [arr1 count]); // arr1 count: 3
- Nsobject * obj1;
- Nsobject * obj2 = [[nsobject alloc] init];
- Nsobject * obj3 = [nsnull null];
- Nsobject * obj4 = [nsobject new];
- Nsarray * arr2 = [nsarray arraywithobjects: obj1, obj2, obj3, obj4, nil];
- Nslog (@ "arr2 count: % lD", [arr2 count]); // arr2 count: 0
Why is there three elements in the first array, and the second element in the array is 0? Let's take a look:
[Objc]View plaincopy
- Nsobject * OBJ;
- If (nil = OBJ ){
- Nslog (@ "obj is nil ");
- }
- Else {
- Nslog (@ "obj is not nil ");
- }
This output is obj is nil. Nsarray ends with nil. So you know why!
(2)
[Objc]View plaincopy
- // An exception occurred!
- Nsobject * obj1 = [nsnull null];
- Nsarray * arr1 = [nsarray arraywithobjects: @ "one", @ "two", obj1, @ "three", nil];
- For (nsstring * STR in arr1 ){
- Nslog (@ "array object: % @", [STR lowercasestring]);
- }
- // Modify
- Nsobject * obj1 = [nsnull null];
- Nsarray * arr1 = [nsarray arraywithobjects: @ "one", @ "two", obj1, @ "three", nil];
- For (nsstring * STR in arr1 ){
- If (! [STR isequal: [nsnull null]) {
- Nslog (@ "array object: % @", [STR lowercasestring]);
- }
- }
Objective-C nil, nil, null and nsnull