Objective-C nil, nil, null and nsnull

Source: Internet
Author: User

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
  1. Nsobject * OBJ = nil;
  2. If (nil = OBJ ){
  3. Nslog (@ "obj is nil ");
  4. }
  5. Else {
  6. Nslog (@ "obj is not nil ");
  7. }

 

 

Ii. Nil: the class is empty.

Define a class as null. For example:

 

[Objc]View plaincopy
  1. Class someclass = nil;
  2. 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
  1. Intint * pointertoint = NULL;
  2. Charchar * pointertochar = NULL;
  3. 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
  1. @ Interface nsnull: nsobject <nscopying, nssecurecoding>
  2. + (Nsnull *) NULL;
  3. @ End

 

The unique method NULL: returns the singleton instance of nsnull is provided in the nsnull Singleton class.

For example:

 

[Objc]View plaincopy
  1. Nsmutabledictionary * mutabledictionary = [nsmutabledictionary dictionary];
  2. Mutabledictionary [@ "somekey"] = [nsnull null]; // sets value of nsnull Singleton for 'somekey'
  3. 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
  1. Nsobject * obj1 = [[nsobject alloc] init];
  2. Nsobject * obj2 = [nsnull null];
  3. Nsobject * obj3 = [nsobject new];
  4. Nsobject * obj4;
  5. Nsarray * arr1 = [nsarray arraywithobjects: obj1, obj2, obj3, obj4, nil];
  6. Nslog (@ "arr1 count: % lD", [arr1 count]); // arr1 count: 3
  7. Nsobject * obj1;
  8. Nsobject * obj2 = [[nsobject alloc] init];
  9. Nsobject * obj3 = [nsnull null];
  10. Nsobject * obj4 = [nsobject new];
  11. Nsarray * arr2 = [nsarray arraywithobjects: obj1, obj2, obj3, obj4, nil];
  12. 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
  1. Nsobject * OBJ;
  2. If (nil = OBJ ){
  3. Nslog (@ "obj is nil ");
  4. }
  5. Else {
  6. Nslog (@ "obj is not nil ");
  7. }

This output is obj is nil. Nsarray ends with nil. So you know why!

 

(2)

 

[Objc]View plaincopy
    1. // An exception occurred!
    2. Nsobject * obj1 = [nsnull null];
    3. Nsarray * arr1 = [nsarray arraywithobjects: @ "one", @ "two", obj1, @ "three", nil];
    4. For (nsstring * STR in arr1 ){
    5. Nslog (@ "array object: % @", [STR lowercasestring]);
    6. }
    7. // Modify
    8. Nsobject * obj1 = [nsnull null];
    9. Nsarray * arr1 = [nsarray arraywithobjects: @ "one", @ "two", obj1, @ "three", nil];
    10. For (nsstring * STR in arr1 ){
    11. If (! [STR isequal: [nsnull null]) {
    12. Nslog (@ "array object: % @", [STR lowercasestring]);
    13. }
    14. }

Objective-C nil, nil, null and nsnull

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.