As per my understanding, NSNull
exists as a replacement of nil
in situations where an object is explicitly required, such in NSArray
and NSDictionary
collections.
One of the good parts of nil
's behavior is that many verbose null
checks can be skipped and it would convenient to have the same behavior implemented by NSNull
. Consider the following example:
NSDictionary * json = // deserialized JSON from a REST service
NSString * name = json[@"first_name"];
if (name != nil && ![name isEqual:[NSNull null]] && [name isEqualToString:@"Gabriele"]) { ... }
the check can currently be simplified to
if (![name isEqual:[NSNull null]] && [name isEqualToString:@"Gabriele"]) { ... }
but if [NSNull null
] was mimicking the behavior of nil
, it could be simplified even more to
if ([name isEqualToString:@"Gabriele"]) { ... }
So my question is why didn't Apple override the -forwardInvocation:
method of NSObject
to mimic the behavior of nil
on an unrecognized selectors?
I would like to get the philosophy behind it and to understand whether there is some drawback or some broken convention that I cannot think of.