0

Possible Duplicate:
Sending a message to nil?

If a reference to NSMutableArray is not pointing to any object at all, because none was instantiated, why can the instance method still be invoked without any run-time error?

NSMutableArray *foo = nil;
NSLog(@"[null count] is %i", [foo count]);

NSLog(@"[null count] again is %i", [(NSMutableArray *) nil count]);

The above lines both print out 0 instead of causing a bad memory access or causing an error that says there is no instance.

Community
  • 1
  • 1
Jeremy L
  • 3,770
  • 6
  • 41
  • 62

1 Answers1

1

Direct from The Objective-C Programming Language: In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime.

If you read on a little further, you'll see why [nil count] returns 0.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74