Summary of Objective-c Nil,nil,null and Nsnull

Source: Internet
Author: User


Summary: nil is used to assign values to objects (any object of object-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (in OBJECT-C class is an object, is an instance of class Meta-class), The nsnull is used for collection operations, although they represent null values, but the use of the situation is completely different, so at the time of encoding strictly according to the variable type to assign values, the correct null value to the correct type, so that the code is easy to read and maintain, it is not easy to cause errors. 1.oc best with nil [nil arbitrary method], will not crash nil is an object value. Null is a general-purpose pointer (generic pointer). 2. Nsnull,null and nil are essentially the same, and null and nil are actually 0, but in objective-c, for types like nsarray, nil or null cannot be an object added to it, If you define a Nsarray, allocate memory for it, and want to set the contents to be empty, you can initialize the content in Nsarray with [NSNULL null], 3. 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;



Because Object-c collection objects, such as Nsarray, Nsdictionary, Nsset, and so on, can contain Nsnull objects, it can cause the program to crash if item in the code is nsnull.








NSString * item = [NSArray objectAtIndex: i];
if ([item isEqualToString: @ "TestNumber"])
{
     //
}

The following code is a common mistake. The release object is not set to nil, which causes the program to crash.
id someObject = [[Object alloc] init];
// ...
[someObject release];
// ...
if (someObject)
{
     // crash here
}


Nil is used to assign values to objects (any object of object-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (in Object-c, class is an object, is an instance of class Meta-class), The nsnull is used for collection operations, although they represent null values, but the use of the situation is completely different, so at the time of encoding strictly according to the variable type to assign values, the correct null value to the correct type, so that the code is easy to read and maintain, it is not easy to cause errors.


// Judge the object is not empty
if (object) ()

// Judge the object is empty
if (object == nil) {}

// Array initialization, null end
NSArray * pageNames = [[NSArray alloc] initWithObjects: @ "DocumentList", @ "AdvancedSearch", @ "Statistics", nil];

// Check if the array element is empty
UIViewController * controller = [NSArray objectAtIndex: i];
if ((NSNull *) controller == [NSNull null])
{
     //
}

// Determine if the elements of the dictionary object are empty
NSString * userId = [NSDictionary objectForKey: @ "UserId"];
if (userId == [NSNull null])
{
     //
} 


Object-c has a lovely feature, that is, when sending a message to a nil object, the system returns a value of 0 instead of causing an exception, which is completely different from Java's annoying nullpointerexception and the process of a C/s program that crashes directly. Understand that this feature of OBJECT-C is important for developing the correct iOS program because nil is the legal value of the object, and the nil object can also receive messages such as:


person=nil;
[person castBallot];
NSLog("person=%@",person);





Summary of 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.