iOS Nil, Nil, NULL, Nsnull detailed

Source: Internet
Author: User
Tags define null instance method


This article mainly introduces the iOS in Nil, Nil, NULL, Nsnull detailed information, the need for friends can refer to the



OBJC inside a few empty value symbols will almost kill me, these basic things should be clear to the line, in order to improve the basic quality of agricultural code.



Nil



A nil is a literal null value for a OBJC object, an object that corresponds to an ID type, or a OBJC object that uses a @interface declaration.



For example:




NSString *somestring = nil; Nsurl *someurl = nil; ID someobject = nil; if (Anotherobject = = nil)//do something


Defined:



Objc.h #ifndef Nil # if __has_feature (cxx_nullptr) # define Nil nullptr # Else # define Nil __darwin_null # endif #endi F//__darwin_null in _types.h #define __DARWIN_NULL ((void *) 0)


Nil



Nil is the written null value of the OBJC class type, corresponding to the class type object.



For example:



Class SomeClass = Nil;



Class Anotherclass = [NSString class];



The definition declaration is similar to the nil, and the values are the same:



Objc.h #ifndef Nil # if __has_feature (cxx_nullptr) # define Nil nullptr # Else # define Nil __darwin_null # endif #endi F


Null



Null is an arbitrary C-pointer null value.



For example:




int *pointertoint = NULL; char *pointertochar = NULL; struct TreeNode *rootnode = NULL;


Defined:




In Stddef.h #define NULL ((void*) 0)


Nsnull



Nsnull is a class that represents a null value and is a OBJC object. In fact, it has only one single instance method: +[nsnull null], which is generally used to represent objects with empty values in the collection.



Example Description:



Because nil is used to mark the end of a collection, nil cannot be stored in the Foundation collection.



Nsarray *array = [Nsarray arraywithobjects:@ "one", @ "two", nil];



Incorrect use of



Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];



[Dict setobject:nil forkey:@ "Somekey"];



The right use



Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];



[Dict setobject:[nsnull null] forkey:@ "Somekey"];



Defined:




/* NSNull.h Copyright (c) 1994-2012, Apple Inc. All rights reserved. * * #import <Foundation/NSObject.h> @interface nsnull:nsobject <nscopying, nssecurecoding> + (nsnull *) null; @end


Summary



Although nil, nil, and NULL values are the same, it is important to understand the written meaning between them, making the code more explicit and more readable.



The above mentioned is the entire content of this article, I hope you can enjoy.


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.