I know it’s ok to send the release message to nil objects. What about other messages? The following code prints 0 to the console. I’d like to understand why.
NSArray *a = nil;
int i = [a count];
NSLog(@"%d", i);
Does sending messages to nil objects ever cause errors?
objc_msgSend()effectively drops messages tonil. If the method has a non-voidreturn type, it will return something likenil, i.e.0,NO, or0.0, although this isn’t always guaranteed all return types on all platforms. Thus, the only errors you’re likely to encounter are when your object isn’t really nil, (e.g. when it’s a reference to a deallocated object), or when you don’t handle nil as a return type appropriately.In your example,
-countreturns anNSUInteger, so the value ofiwill be0, sinceobjc_msgSend()will return0for a message tonilthat should return anNSUInteger.