Definitions and differences of NULL, nil, nil, NSNull in Objective-c

Source: Internet
Author: User
Tags define null

Source: Xcodemen (Kang Zubin)

Links: http://www.jianshu.com/p/5d7033b15052

This article is written by our team of Kang Zubin Children's shoes, which is his personal homepage: https://kangzubin.cn.

Understanding the concept of "non-existent" is not only a philosophical problem, but also a practical one. We are the inhabitants of the physical universe, and the reason lies in the uncertainty of the existence of the logical universe. As a physical embodiment of a logical system, the computer faces a difficult problem, that is, how to use "existence" expression "does not exist." – Excerpt from Nshipster

This passage is strange to read, after all, is translated, probably means that in the computer how to describe the concept of "not exist" is very important.

In the C language, it is used as the original value of "nonexistent", and NULL as the pointer null value. In Objective-c, there are several different ways to express "nonexistent", respectively: NULL, nil, nil, NSNull. Let's look at the definitions of these null values and the difference in usage.

Note: The following types of null-defined sources are extracted from the relevant header files in the IOS 10.0 SDK.

Null

The NULL definition is in the usr/include/sys/_types/_null.h file:

#ifndef NULL

#define NULL __darwin_null

#endif/* NULL */

Where __darwin_null is defined in the Usr/include/sys/__types.h file, as follows:

#ifdef __cplusplus

# ifdef __GNUG__

# define __darwin_null __null

# Else/*! __gnug__ * *

# ifdef __LP64__

# define __DARWIN_NULL (0L)

# Else/*!__lp64__ */

# define __DARWIN_NULL 0

# endif/* __lp64__ * *

# endif/* __gnug__ * *

#else/*! __cplusplus * *

# define __DARWIN_NULL ((void *) 0)

#endif/* __cplusplus */

The code above first defines the value of the __darwin_null of the different compilers in the C + + environment and then defines the values for the __darwin_null in the other environment, so the final definition of NULL in Objective-c is:

#define NULL ((void*) 0)

That is, NULL is essentially: (void*) 0.

Usage conventions: null is generally used to represent null values for C pointers, for example:

int *pointertoint = NULL;

Char *Pointertochar = NULL;

struct TreeNode *rootNode = NULL;

Nil

Nil is defined in the Usr/include/objc/objc.h file:

#ifndef Nil

# if __has_feature (cxx_nullptr)

# define NIL nullptr

# Else

# define NIL __darwin_null

# endif

#endif

where __has_feature (cxx_nullptr) is used to determine whether the current environment has C + + nullptr characteristics, if any, nil is defined as nullptr, otherwise nil is defined as __darwin_null, so in objective-c The final definition of nil is:

#define NIL ((void*) 0)

In other words, nil is essentially: (void *) 0, which is identical to NULL.

Usage conventions: Nil is used to indicate that a pointer to an Objective-c object (an object of type ID or an OC object that uses @interface declaration) is empty, for example:

NSString *somestring = nil;

Nsurl *Someurl = nil;

ID someobject = nil;

If (anotherobject = = nil) //do something

Nil

Nil is defined in the Usr/include/objc/objc.h file:

#ifndef Nil

# if __has_feature (cxx_nullptr)

# define Nil nullptr

# Else

# define Nil __darwin_null

# endif

#endif

In accordance with the above nil, nil is essentially: (void *) 0.

Usage Convention: Nil is used to indicate that a pointer to the Objective-c class type (Class) is empty, for example:

Class SomeClass = Nil;

Class Anotherclass = [nsstring class];

NSNull

NSNull is defined in the NSNull.h file:

#import

Ns_assume_nonnull_begin

@Interface NSNull : nsobject

+ (NSNull *)null;

@End

Ns_assume_nonnull_end

From the above definition, we know that NSNull is a Objective-c object, a class for representing null values, and it has only one singleton method: +[nsnull null], which is typically used to hold an empty placeholder object in the collection object.

Usage conventions: In Foundation Collection objects (Nsarray, Nsdictionary, Nsset, and so on), nil is typically used to represent the end of a collection object, so it is not possible to store a null value with nil, so it is generally stored with a [NSNull null] empty object. In addition, in Nsdictionary's-objectforkey: method, if the value of key in the current dictionary does not exist, the method returns nil, indicating that the current key is not added in the dictionary, but if we want to make it clear that a key has been added to the dictionary, it does not have a value , you can use [NSNull null] to assign a value representation.

When nil is encountered in Nsarray, it means that the element of the array object is cut off, that is, Nsarray only focuses on the object before nil, and the object after nil is discarded.

Nsarray *Array = [nsarray arraywithobjects:@"One", @"One", Nil];

Use of Errors

Nsmutabledictionary *dict = [nsmutabledictionary dictionary];

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

The right use

Nsmutabledictionary *dict = [nsmutabledictionary dictionary];

[dict setobject:[NSNull null] forkey:@"Somekey"] ;

NIL or Nsnil

These two symbols do not exist in OBJECTIVE-C!!!

Summarize

From the above analysis, we can know that whether it is NULL, nil or nil, they are essentially the same, are (void *) 0, but the wording is different. The significance of this is to differentiate between different data types, although they are the same value, but we need to understand the literal meaning between them and use them for different scenarios, making the code more explicit and more readable.

logo value meaning
Null (void *) 0 The literal null value of the C pointer
Nil (ID) 0 The literal null value of the Objective-c object
Nil (Class) 0 Literal null value of the Objective-c class
NSNull [NSNull NULL] The Objective-c object used to represent the null value

Reference

      • Nil/nil/null/nsnull

        http://nshipster.cn/nil/

      • Difference between nil, nil and, NULL in Objective-c

        Http://stackoverflow.com/questions/5908936/difference-between-nil-nil-and-null-in-objective-c

      • The difference between nil/nil/null/nsnull

        http://blog.csdn.net/wzzvictory/article/details/18413519

      • Nil,nil,null and Nsnull Understanding in Objective C

        http://magicalboy.com/null-value-in-objective-c/

Definitions and differences of NULL, nil, nil, NSNull in Objective-c

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.