NIL: A null pointer to an object that assigns a null value to the objective C ID object.
Nil: A null pointer to a class that represents a null value for the class.
NULL: A null pointer to another type (for example, base type, C type) to assign a null value to a non-object pointer.
NSNull: An object in the collection object that represents a null value.
1.nil the pointer to an object is null defined as follows:
#ifndef nil# if __has_feature(cxx_nullptr)# define nil nullptr# else# define nil __DARWIN_NULL# endif#endif
Object in objective-c for ID type
NSString *str = nil;NSURL *url = nil;id object = nil;
2.Nil pointers to a class are null defined as follows:
#ifndef Nil# if __has_feature(cxx_nullptr)# define Nil nullptr# else# define Nil __DARWIN_NULL# endif#endif
Objects for class type in Objective-c
Class Class1 = Nil;Clsss Class2 = [NSURL class];
3.NULL pointer to type C is empty as defined in Stddef.h:
#If defined (__need_null)#undef NULL#ifdef __cplusplus# if!defined (__mingw32__) &&!defined (_msc_ver)# define NULL __null# Else# define NULL 0# endif#Else# define NULL ((void*)0)#endif
Simple example for assigning null values to non-object pointers
int *intA = NULL;char *charC = NULL;struct structStr = NULL;
4.NSNull is a class in objective-c.
NSNull has + (NSNull *) null; Single-instance method.
Many are used for objects with null values in the collection (Nsarray,nsdictionary).
NSArray *array = [NSArray arrayWithObjects: [[NSObject alloc] init], [NSNull null], @"aaa", nil, [[NSObject alloc] init], [[NSObject alloc] init], nil];NSLog(@"%ld", array.count); // 输出 3,NSArray以nil结尾
//
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @"Object0", @"Key0", @"Object1", @"Key1", nil, @"Key-nil" @"Object2", @"Key2", nil];NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾
//
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];[mutableDictionary setObject:nil forKey:@"Key-nil"]; // 会引起Crash[mutableDictionary setObject:[NSNull null] forKey:@"Key-nil"]; // 不会引起Crash//所以在使用时,如下方法是比较安全的[mutableDictionary setObject:(nil == value ? [NSNull null] : value) forKey:@"Key"];
Wen/jasomzl (author of Jane's book)
Original link: http://www.jianshu.com/p/c01e875686d3
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
The difference between nil, nil, NULL, Nsnull