Are NULL and nil equivalent?
Asked Answered
C

6

17

Actually my question here is: are null and nil equivalent or not?

I have an example but I am confused when they are equal when they are not.

NSNull *nullValue = [NSNull null];
NSArray *arrayWithNull = [NSArray arrayWithObject:nullValue];
NSLog(@"arrayWithNull: %@", arrayWithNull);
id aValue = [arrayWithNull objectAtIndex:0];

if (aValue == nil) {
    NSLog(@"equals nil");
} else if (aValue == [NSNull null]) {
    NSLog(@"equals NSNull instance");
    if ([aValue isEqual:nil]) {
        NSLog(@"isEqual:nil");
    }
}

Here in the above case it shows that both null and nil are not equal and it displays "equals NSNull instance"

NSString *str=NULL;
id str1=nil;
if(str1 == str)
{
   printf("\n IS EQUAL........");

}
else
{
    printf("\n NOT EQUAL........");
}

And in the second case it shows both are equal and it displays "IS EQUAL".

Anyone's help will be much appreciated.

Thank you, Monish.

Columniation answered 17/3, 2010 at 10:56 Comment(2)
The code here bears a striking resemblance to this article: developer.apple.com/mac/library/documentation/cocoa/Conceptual/…Feline
possible duplicate of When to use nil and NULL in Objective C?Coycoyle
S
22

nil and NULL are essentially the same, nil is something like (NSObject *)0, while NULL is more like (void *)0. But both are pointers with an integer value of zero. You can send messages to nil without raising an error.

NSNull and NULL (or nil, of course) are different things, however. You just use NSNull as a helper to add an empty object to an NSArray or another container class, since you can't add nil to them. So instead, you use [NSNull null] as a replacement, and you have to check if an array element is NSNull, not if it's nil (it will never be equal to nil).

Sonority answered 17/3, 2010 at 11:9 Comment(4)
Apparently according to the Apple Docs NSNull is Not (NSObject *)0 but is a valid instance.Feline
Does that mean that (str1==str) is using an operator-overload for == which is a value comparison instead of an identity comparison? That might be the source of confusion leading to the original question.Feline
Indeed, NSNull is a valid object. That's what I wanted to say. There is no operator overloading in Objective C, by the way. Comparisons of Objective C objects are just pointer comparisons.Sonority
@Warren P: FWIW, you can use == to compare to [NSNull null] only because it's guaranteed to always return the same object instance.Veronikaveronike
K
7

From http://www.iphonedevsdk.com/forum/iphone-sdk-development/34826-nil-vs-null.html

nil and NULL are 100% interchangeable.

From:

  • NULL is for C-style memory pointers.
  • nil is for Objective-C objects.
  • Nil is for Objective-C classes.

Whenever you're writing Objective-C code, use nil Whenever you're writing C code, use NULL

But ultimately they're all defined as the same thing -- (void *)0, I think -- so in practice it doesn't really matter.

Kizzee answered 3/7, 2010 at 4:16 Comment(4)
NSNull is not the same as NULL.Windpipe
You're right. [NSNull null] is a singleton value used to represent a nil value in a situation which prohibits the use of nil -- for example in a collection class (like NSDictionary, NSArray, NSSet, or mutable variants).Kizzee
In other words, it's used as a placeholder in collections which do not allow you to set an item to nil.Kizzee
Can I use nil in C code in a Objective-C implementation file. Since you can combine them, can I combine nil and NULL? Just to be sure. :-)Stewardson
I
2

The concept is the same, with the difference that it's valid to send messages (call method) to nil.

NSNull is a real (singleton) class, that can be used for arrays or dictionnaries, who don't accept NULL or nil values.

Interplanetary answered 17/3, 2010 at 11:7 Comment(0)
Q
2

Biggest difference between them: sending a message to an NSNULL object is probably going to cause a crash, whereas it's cool to send any message to nil. For example, if you use a key path to get an array, like so:

NSArray *departmentNames = [departments valueForKey:@"name"];

Then you will have an NSNULL object for any department whose name is nil. So, this is going to cause a crash:

for (NSString *name in departmentNames)
    NSLog(@"%@", [name lowercaseString]);

whenever name is NSNull, because you just sent an unknown selector (lowercaseString) to an NSNull.

Lesson: check for the NSNull object in an array before sending any message to its elements.

for (NSString *name in departmentNames)
    if (name != [NSNull null])
         NSLog(@"%@", [name lowercaseString]);
Quiescent answered 30/1, 2012 at 20:12 Comment(0)
H
1

No, NSNull and nil are not the same. They both represent a lack of value, and you might want to treat them the same, but they are still not equal.

The NSNull object instance represents a null value, for example when you read data from a database that has null values.

The nil value is a null pointer, i.e. it doesn't point to any object instance.

In your second code you don't have any NSNull instance. An NSString pointer that contains a null pointer is not an NSNull instance, it's still just a null pointer. You are comparing one null pointer to another, and they are of course equal.

Hus answered 17/3, 2010 at 11:12 Comment(0)
L
1

Make sure you typecast [NSNull null] to object type that you are comparing

NSArray list;
if(list==(NSArray *)[NSNull null])
    // do something

otherwise you will receive a warning message saying "Comparison of distinct pointer types('type *' and 'NSNull *')

Lesleelesley answered 9/10, 2012 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.