Some time ago, in the development process, some inexplicable places on the interface show < null > and some places show < null > which is very painful to modify. After searching for information to summarize the subtleties, so as not to spend meaningless time on this thing to modify.
First of all, I will record the solutions to these problems. The most simple and direct way is to locate the problem first, and then print out the variable in a violent way.
There are two types of printing: 1) print address at% p; 2) description of print object (string object is itself)
This article first talks about the conclusion and then unfolds it.
Summary: 0. nil, NULL are essentially the same, all point to 0x0 address, [NSNULL null] is an object, stored in the constant area, occupying a fixed address.
1.nil denotes that an object pointed by a pointer is empty, and the object type is id, which shows that it is (null) - > common in non-set classes.
2.[NSNull null] denotes the empty object itself and shows that <null> - ------------------------------------> is common in collection classes.
3.NULL is not substantially different from nil, except that the former is only in C.
Here's a detailed explanation of the three differences
============== nil ================
-
nil is the literal null value of an OC object whose type is id.
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] 0x0
2015-10-06 13:13:45.338 test[95730:5489376] 0x0
2015-10-06 13:13:45.338 test[95730:5489376] 0x0
2015-10-06 13:13:45.338 test[95730:5489376] 1
2. Empty objects are printed on the console as null.
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 ================
-
NULL is an arbitrary null C-pointer
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] 1
2015-10-06 13:38:59.927 test[95925:5515192] 1
2. The console prints out (null)
3. definition
#ifndef NULL
#define NULL __DARWIN_NULL
#endif /* ! NULL */
===========NSNULL ================
-
NSNULL is a class that represents null values
-
NSNULL has only one singleton method [NSNull null]
-
Purpose: Objects with empty set values (nil in oc cannot be stored in a collection because nil is the tag bit at the end of the collection)
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] one
2015-10-06 16:40:25.817 test[96177:5565855] two
2015-10-06 16:40:25.817 test[96177:5565855] <null>
4. At this point, the console prints out <null>.
5. definition
/* NSNull.h
Copyright (c) 1994-2015, Apple Inc. All rights reserved.
*/
#import <Foundation/NSObject.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSNull : NSObject <NSCopying, NSSecureCoding>
+ (NSNull *)null;
@end
NS_ASSUME_NONNULL_END
Reference material: 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