Difference between nil, NULL and Nil in iOS
Let us understand the concept of nothing in the iOS world. It is easy to understand compared to the same philosophy in Real world!!!
In programming we need something to represent nothing.
nil, Nil and NULL all are used to represent the absence of value.
They are all zero but they differ in their types.
NULL is a void *, nil is an id, and Nil is a Class pointer.
It means that nil is an empty value bound with an object (the id type in Objective-C).
Nil is used to represent a null pointer to an Objective-C class.
NULL is used to represent a null pointer to anything else.
Let’s see some basic examples to get better idea:
NSString *nothing= nil;
NSURL *someURL = nil;
id someObject = nil;
if (anotherObject == nil) // do something
Nil is the literal null value for Objective-C classes, corresponding to the type Class.
Class someClass = Nil;
Class anotherClass = [NSString class];
NULL is the literal null value for arbitrary C pointers. For instance,
int *pointerToInt = NULL;
char *pointerToChar = NULL;
struct TreeNode *rootNode = NULL;
It is also important to note that nil in Swift is not the same as nil in Objective-C.
In Objective-C, nil is a pointer to a non-existent object. In Swift, nil is not a pointer but the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types.
NULL and Nil have no equivalent in Swift.
I will add more examples and other significant differences to get more clarity if I get anything related to this.
Thanks for reading!!!
Refrences: