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.