IOS (null), & lt; null & gt;, nil, iosnil

Source: Internet
Author: User

IOS (null), <null>, nil problems, iosnil
Abstract: Have you ever experienced something like <null> or (null. I have already said this again. Today I will summarize the solutions to this problem.

Some time ago, during the development process, some of the interface was inexplicably displayed in some places (null) and some areas showed <null>. The modification was very painful. After searching for information, I summarized the details, so that you do not need to spend any time on this item.

 

First, record the problem-solving ideas I encountered. The simplest and most direct method is: first locate the problem and print the variable in brute force mode!

There are two types of printing: ① % p print address; ② % @ print the description of the object (the string object is its own)

This article begins with the conclusion and expands.

Conclusion: 0. nil and NULL are essentially the same and all point to the 0x0 address. [NSNULL null] is an object stored in the constant area and occupies a fixed address.

1. nil indicates that the object pointed to by a pointer is null. The object type is id and is displayed as (null) ---> common in non-collection classes

2. [NSNull null] indicates the empty object, which is displayed as <null> ------------------------> common in the Collection class.

3. There is no substantive difference between NULL and nil, except that the former is only in the C Language

The following describes the differences among the three methods in detail.

================== Nil ============================

 NSString *str = nil; NSData *data = nil;  NSLog(@"%@",nil); NSLog(@"%@",str);   NSLog(@"%@",data);   NSLog(@"%p",nil); NSLog(@"%p",str);   NSLog(@"%p",data);  NSLog(@"%d",(data == nil)); 2015-10-06 13:13:45.338 test[95730:5489376] (null)2015-10-06 13:13:45.338 test[95730:5489376] (null)2015-10-06 13:13:45.338 test[95730:5489376] (null)2015-10-06 13:13:45.338 test[95730:5489376] 0x02015-10-06 13:13:45.338 test[95730:5489376] 0x02015-10-06 13:13:45.338 test[95730:5489376] 0x02015-10-06 13:13:45.338 test[95730:5489376] 1

2. The empty object is (null) printed on the console)

3. nil Definition

// objc.h#ifndef NULL#define NULL    __DARWIN_NULL#endif /* ! NULL */#ifndef nil  #if defined(__has_feature)     #if __has_feature(cxx_nullptr)      #define nil nullptr    #else      #define nil __DARWIN_NULL    #endif  #else    #define nil __DARWIN_NULL  #endif#endif// __DARWIN_NULL in _types.h #define __DARWIN_NULL ((void *)0)

============= NULL ==========================

int *pointerToInt = NULL;char *pointerToChar = NULL;struct TreeNode *rootNode = NULL;NSLog(@"%@",pointerToInt);NSLog(@"%s",pointerToChar); NSLog(@"%@",rootNode);NSLog(@"%d",pointerToInt==NULL);  //NSLog(@"%d",pointerToInt==nil);   //2015-10-06 13:38:59.927 test[95925:5515192] (null)2015-10-06 13:38:59.927 test[95925:5515192] (null)2015-10-06 13:38:59.927 test[95925:5515192] (null)2015-10-06 13:38:59.927 test[95925:5515192] 12015-10-06 13:38:59.927 test[95925:5515192] 1

2. the console prints it as (null)

3. Definition

#ifndef NULL#define NULL    __DARWIN_NULL#endif /* ! NULL */

============= NSNULL ============================

 

 NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",[NSNull null], nil];        for (NSString *str in arr) {            NSLog(@"%@",str);        }2015-10-06 16:40:25.816 test[96177:5565855] one2015-10-06 16:40:25.817 test[96177:5565855] two2015-10-06 16:40:25.817 test[96177:5565855] <null>

4. What is printed on the console at this time is <null>

5. Definition

/*NSNull.hCopyright (c) 1994-2015, Apple Inc. All rights reserved.*/#import <Foundation/NSObject.h>NS_ASSUME_NONNULL_BEGIN@interface NSNull : NSObject <NSCopying, NSSecureCoding>+ (NSNull *)null;@endNS_ASSUME_NONNULL_END

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

Http://blog.csdn.net/shenshen123jun/article/details/38315755

Https://github.com/nicklockwood/NullSafe

 

 

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.