Does NULL and nil are equal?

Posted by monish on Stack Overflow See other posts from Stack Overflow or by monish
Published on 2010-03-17T10:56:26Z Indexed on 2010/03/17 11:01 UTC
Read the original article Hit count: 334

Filed under:

Hi Guys,

      Actually my question here is does Null and nil are equal or not?

I had 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.

© Stack Overflow or respective owner

Related posts about objective-c