As others have pointed out, object != nil && object != NSNull.null would give you the expected behaviour, however it might be tedious and error prone to write this pair of conditions every time.
Alternatively you can use inverted logic, by adding an nonNullValue method to virtually almost all objects in the system:
// in some header file
@interface NSObject(NullExtension)
- (instancetype)nonNullValue;
@end
// in some implementation file
@implementation NSObject(NullExtension)
// regular objects are not null (right?)
- (instancetype)nonNullValue {
return self;
}
@end
@implementation NSNull(NullExtension)
// let's make NSNull report as being null
- (instancetype)nonNullValue {
return nil;
}
@end
// adding this just for the sake of completeness, it's highly unlikely
// that JSON's will decode NSProxy instances
@implementation NSProxy(NullExtension)
- (instancetype)nonNullValue {
return self;
}
@end
you can then simply use it on your pointers:
// since we're in Objective-C, a nil pointer will always fail the check
// and thanks to the NSNull extension, NSNull instances will behave the same
if ([object nonNullValue] != nil) {
// NSNull won't get here
// nils won't get here
// other objects will get here
}
This approach is a little bit invasive as it touches all NSObject subclasses, however it eliminates the need of writing multiple conditions.
nilis nothing & you cannot check the value ofnothing, in contrastNSNullis literally an object & its value isnil.