Sending messages to nil

Nearly all object-oriented languages have the idea of nil, the pointer to no object. In Objective-C, we use nil instead of NULL, which was discussed in Chapter 9. They really are the same thing: the zero pointer. By convention, though, we use nil when referring to the value of an empty pointer declared as pointing to an Objective-C object type, and NULL when referring to any other pointer, such as to a struct.

In most object-oriented languages, sending a message to nil is not allowed. As a result, you have to check for non-nil-ness before accessing an object. So you see this sort of thing a lot:

i​f​ ​(​f​i​d​o​ ​!​=​ ​n​i​l​)​ ​{​
 ​ ​ ​ ​[​f​i​d​o​ ​g​o​G​e​t​T​h​e​N​e​w​s​p​a​p​e​r​]​;​
}​

When Objective-C ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.