Is NSNull equal to NSNull

546 views Asked by At

Is [[NSNull null] isEqual:[NSNull null]] == YES?

What is the proper way to test if an id is NSNull. As well, if checking two collections for equality and they both contain NSNulls, will they still be considered equal?

1

There are 1 answers

2
Carl Norum On BEST ANSWER

Assuming you meant:

[[NSNull null] isEqual:NSNull]

No, NSNull is a class, not an object. You can't meaningfully compare it to [NSNull null]. You will always get the same object back from [NSNull null], though - it's a singleton. From the documentation:

+ (NSNull *)null
Return Value
The singleton instance of NSNull.

So that means this expression:

[NSNull null] == [NSNull null]

Always returns 1. Likewise:

[[NSNull null] isEqual:[NSNull null]]

Will return YES.

Example:

#include <Foundation/Foundation.h>

int main(void)
{
    @autoreleasepool
    {
        NSLog(@"%d", [[NSNull null] isEqual:[NSNull null]]);
        NSLog(@"%d", [NSNull null] == [NSNull null]);
    }
    return 0;
}

Building & running:

$ clang example.m -o example -framework Foundation
$ ./example 
2013-08-30 14:56:21.200 example[27901:707] 1
2013-08-30 14:56:21.201 example[27901:707] 1