Explanation of nil, Nil, NULL, and NSNull in iOS, nilnsnull
Nil
- Nil is the literal null value of the ObjC object, corresponding to the id type object, or the ObjC object declared by @ interface.
- For example:
NSString *someString = nil;NSURL *someURL = nil;id someObject = nil; if (anotherObject == nil) // do something
- Definition:
// objc.h#ifndef nil# if __has_feature(cxx_nullptr)# define nil nullptr# else# define nil __DARWIN_NULL# endif#endif // __DARWIN_NULL in _types.h #define __DARWIN_NULL ((void *)0)
Nil
NULL
NSNull
- NSNull is a class that represents null values and is an ObjC object. In fact, it only has one Singleton method: + [NSNull null], which is generally used to indicate objects with null values in the set.
- Example:
// Nil cannot be stored in the Foundation set because nil is used to end the set. NSArray * array = [NSArray arrayWithObjects: @ "one", @ "two", nil]; // NSMutableDictionary * dict = [NSMutableDictionary]; [dict setObject: nil forKey: @ "someKey"]; // use NSMutableDictionary correctly * dict = [NSMutableDictionary dictionary]; [dict setObject: [NSNull null] forKey: @ "someKey"];
- Definition:
/* NSNull.h Copyright (c) 1994-2012, Apple Inc. All rights reserved.*/ #import <Foundation/NSObject.h> @interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end
Summary:Nil: NULL pointer to an object
Nil: NULL pointer to a class
NULL: NULL pointer to other types (such as basic type and C type)
NSNull: Null Value in the set.