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.
nilandNULLare essentially the same,nilis something like(NSObject *)0, whileNULLis more like(void *)0. But both are pointers with an integer value of zero. You can send messages tonilwithout raising an error.NSNullandNULL(ornil, of course) are different things, however. You just useNSNullas a helper to add anemptyobject to anNSArrayor another container class, since you can’t addnilto them. So instead, you use[NSNull null]as a replacement, and you have to check if an array element isNSNull, not if it’snil(it will never be equal tonil).