The difference between nil nil null in iOS

Source: Internet
Author: User

Nil Nil NULL NSNull

NIL:A null pointer to an OBJECTIVE-C object. (#define NIL ((ID) 0))

Nil is an object value.

NIL:A null pointer to an OBJECTIVE-C class.

NULL:A null pointer to anything else. (#define NULL ((void *) 0))

Null is a general-purpose pointer (generic pointer).


Nsnull:a class defines A Singleton object used to represent null values in collection objects (which don ' t allow Nil values).
[NSNull NULL]: The singleton instance of NSNull.

[NSNull NULL] is an object that he uses in situations where nil can not be used.

Because there is a special meaning in nil in Nsarray and nsdictionary (indicating the end of the list), you cannot put a nil value in the collection. If you really need to store a value that says "Nothing", you can use the Nsnull class. There is only one way to Nsnull:

+ (NSNull *) null;

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 are recommended, but not required.

If someone sees you passing NULL, they know the receiver expects a C pointer. If They see nil, they know the receiver was expecting an object. If They see Nil, they know the receiver is expecting a class. Readability.

Example 1:

[Nsapp Beginsheet:sheet
Modalforwindow:mainwindow

Modaldelegate:nil//pointing to an object

Didendselector:null//pointing to a non object/class

Contextinfo:null]; Pointing to a non object/class



Example 2:the values1 is only having obj1,
NSObject *obj1 = [NSObject new];
NSObject *obj2 = nil;
NSObject *obj3 = [NSObject new];
Nsarray *values1 = [Nsarray arraywithobjects:obj1,obj2,obj3,nil];

Example 3:the Values2 has 3 objects and the second object is instance of Nusnull
NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSNull null];
NSObject *obj3 = [NSObject new];
Nsarray *values2 = [Nsarray arraywithobjects:obj1,obj2,obj3,nil];

About testing:
If obj is nil, [obj message] would return NO, without nsexception
If obj is NSNull, [obj message] would throw a nsexception
Testing NSNull: (NSNull *) obj = = [NSNull null]

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.