Difference between nil, NIL, and null in Objective-C
Asked Answered
U

6

76

I want to know the difference between nil, NIL, and null. I've googled around and found this:

nil -> null pointer to Objective-C object

NIL -> null pointer to Objective-C class

null -> null pointer to primitive type or absence of data

But I'm not able to understand the terms "Objective-C object" and "class" clearly.

Please explain this to me. Also, is there any word like NSNull or NSNil in Objective-C? If so, then please explain for what it is for.

Unvalued answered 6/5, 2011 at 8:45 Comment(2)
An Objective-C object is an instance of an Objective-C class (the one which is declared via @interface). The instance is usuallay created with [[MyClass alloc] init] or [MyClass new].Greasy
In ObjC difference between object and class is the same as in any other language. If you don't know that - you should learn the OOP basics.Cogwheel
H
157

nil is the literal null value for Objective-C objects, corresponding to the abstract type id or any Objective-C type declared via @interface. For instance:

NSString *someString = 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. Since most code doesn’t need variables to reference classes, its use is not common. One example is:

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;

NSNull is a class for objects that represent null. In fact, there’s only one object, namely the one returned by +[NSNull null]. It is different from nil because nil is a literal null value, i.e., it isn’t an object. The single instance of NSNull, on the other hand, is a proper object.

NSNull is often used in Foundation collections since they cannot store nil values. In the case of dictionaries, -objectForKey: returns nil to indicate that a given key has no corresponding object in the dictionary, i.e., the key hasn’t been added to the dictionary. If you want to make it explicit that you have a certain key but it doesn’t have a value yet, you can use [NSNull null].

For instance, the following throws an exception because dictionaries cannot store nil values:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:nil forKey:@"someKey"];

On the other hand, the following code is valid since [NSNull null] is a non-nil object:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNull null] forKey:@"someKey"];

It’s worth mentioning that Foundation collections have initialisers that use nil as a marker for the end of a list of objects without having to specify the number of elements in the list. This can only happen because nil cannot be stored in a Foundation collection. For instance,

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil];

As for NIL or NSNil, there are no such things in Objective-C or Apple Foundation.

Hescock answered 6/5, 2011 at 9:19 Comment(3)
You should also note that it is possible to send messages to nil without getting segmentation faults.Greasy
nice explaination thanks. but i would like to ask that can we use NULL for objective C objects?? like---> id mYobj = NULL ??Weitzel
This can be reflected to swift? Like, is in swift a correspondence for that?Auscultate
T
7

I am not sure but i think nil should only be used in place of an id, what Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers.

nil is usually used for an Objective-C object type, while NULL is used for c-style pointers

Toxicology answered 6/5, 2011 at 8:53 Comment(2)
This is correct. I think in practice it doesn't really matter which you use. But follow the convention.Columnar
The convention is useful but it's good to know that nil and NULL and Nil are all the same to the compiler.Dolly
A
7

Nil,Null and nil are used with below

1> Nil for Objective c Class

2> nil for Objective c object

3> Null for C pointer

Example:

1>Class A=Nil;

2>NSString strName=nil;

3>char *pointerChar = NULL;

Aras answered 19/2, 2014 at 6:35 Comment(1)
elegant example buddyHuxham
A
5

Suppose you have a class MyClass then by convention nil is used if you want to initialize its instance to null value (same as null in java) i.e.

MyClass *obj = nil;


and if you want to initialize a primitive pointer to null value (same as in c) you use

int *ptr = NULL; 


and if you want to initialize to Class reference to null value (same as null in java) then use

Class classRefOfMyClass = Nil;

It's just a convention otherwise Nil or nil have same meaning and perhaps NULL , nil or Nil all are same.

Here is the definition for these in objc.h file

#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif

And in stddef.h

#define NULL ((void*)0)

And the definition of __DARWIN_NULL in _types.h

#define __DARWIN_NULL ((void *)0)

So there is no difference logically. The main idea here is to initialize a pointer whether C or Objective-C to 0. If you have knowledge of C then you can assign

int *ptr = 0;

without type casting 0 to a pointer. As you don't need to typecast 0 to assign it to a pointer.

In short they all are 0 and nothing else.

Associationism answered 6/5, 2011 at 9:17 Comment(1)
Java's NULL is in lower-case: nullArakawa
S
4

This will help you to understand the difference between nil,NIL and null.

All three of these values represent null, or zero pointer, values. The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers. It should be considered a best practice of sorts to use the right null object in the right circumstance for documentation purposes, even though there is nothing stopping someone from mixing and matching as they go along.

The below link may help you in some way:

http://nshipster.com/nil/

Here is some important part from the link:

enter image description here

Salicylate answered 10/9, 2014 at 9:3 Comment(0)
T
1

nil, NIL and null. is depended on your requirement.

NSNull

collections like NSArray and NSDictionary not being able to contain nil values.

NSMutableDictionary *MymutableDictionary = [NSMutableDictionary dictionary];
MymutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for "someKey"
NSLog(@"Keys: %@", [mutableDictionary allKeys]);

nil

all pointers that object has to other objects begin as nil, so it's unnecessary to, for instance, set self.(association) = nil in init methods.

In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value.

if (name != nil)
{
........
}

Symbol Value Meaning

nil (id)0 literal null value for Objective-C objects

Nil (Class)0 literal null value for Objective-C classes

Tallage answered 18/6, 2015 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.