Nil is for object pointers, NULL is for non pointers, Null and Nil both defined to be equal to the value zero. NULL is a void *, nil is an id, and Nil is a Class pointer, NULL is used for non-object pointer (like a C pointer) in Objective-C.
C represents nothing as 0 for primitive values and NULL for pointers (which is equivalent to 0 in a pointer context). The only time you see NULL in Objective-C code is when interacting with low-level C APIs like Core Foundation or Core Graphics.
The NULL value for Objective-C objects (type id) is nil. While NULL is used for C pointers (type void *). nil = (all lower-case) is a null pointer to an Objective-C object. Nil = (capitalized) is a null pointer to an Objective-C class.
nil is an Object, NULL is a memory pointer Sadly, when this happens, Ruby developers are confusing a simple little Ruby object for something that's usually radically different in “blub” language. Often, this other thing is a memory pointer, sometimes called NULL, which traditionally has the value 0.
They differ in their types. They're all zero, but NULL
is a void *
, nil
is an id
, and Nil
is a Class pointer.
You can use nil
about anywhere you can use null
. The main difference is that you can send messages to nil
, so you can use it in some places where null
cant work.
In general, just use nil
.
nil is an empty value bound/corresponding with an object (the id type in Objective-C). nil got no reference/address, just an empty value.
NSString *str = nil;
So nil should be used, if we are dealing with an object.
if(str==nil)
NSLog("str is empty");
Now NULL is used for non-object pointer (like a C pointer) in Objective-C. Like nil , NULL got no value nor address.
char *myChar = NULL;
struct MyStruct *dStruct = NULL;
So if there is a situation, when I need to check my struct (structure type variable) is empty or not then, I will use:
if (dStruct == NULL)
NSLog("The struct is empty");
Let’s have another example, the
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
Of key-value observing, the context should be a C pointer or an object reference. Here for the context we can not use nil; we have to use NULL.
Finally the NSNull class defines a singleton object used to represent null values in collection objects(NSArray, NSDictionary). The [NSNull null] will returns the singleton instance of NSNull. Basically [NSNull null] is a proper object.
There is no way to insert a nil object into a collection type object. Let's have an example:
NSMutableArray *check = [[NSMutableArray alloc] init];
[check addObject:[NSNull null]];
[check addObject:nil];
On the second line, we will not get any error, because it is perfectly fair to insert a NSNull object into a collection type object. On the third line, we will get "object cannot be nil" error. Because nil is not an object.
NULL and nil are equal to each other, but nil is an object value while NULL is a generic pointer value ((void*)0
, to be specific). [NSNull null]
is an object that's meant to stand in for nil in situations where nil isn't allowed. For example, you can't have a nil value in an NSArray. So if you need to represent a "nil", you can use [NSNull null]
.
I've found the following:
objc.h
#define Nil __DARWIN_NULL /* id of Nil class */
#define nil __DARWIN_NULL /* id of Nil instance */
_types.h
#define __DARWIN_NULL ((void *)0)
stddef.h
#undef NULL
#ifdef __cplusplus
#undef __null // VC++ hack.
#define NULL __null
#else
#define NULL ((void*)0)
#endif
MacTypes.h
#ifndef NULL
#define NULL __DARWIN_NULL
#endif /* ! NULL */
#ifndef nil
#define nil NULL
#endif /* ! nil */
The way it looks, there's no difference but a conceptual one.
Beware that if([NSNull null])
returns true
.
They both are just typecast zero's. Functionally, there's no difference between them. ie.,
#define NULL ((void*)0)
#define nil ((id)0)
There is a difference, but only to yourself and other humans that read the code, the compiler doesn't care.
One more thing nil is an object value while NULL is a generic pointer value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With