01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

62 CHAPTER 3 An introduction to objects<br />

Indicating the absence of a value<br />

Sometimes you want to detect if a pointer variable is currently pointing at anything of<br />

relevance. For this purpose, you’ll most likely initialize the pointer to one of the special<br />

constants NULL or nil:<br />

int *x = NULL;<br />

NSString *y = nil;<br />

Both constants are equivalent to the value 0 and are used to indicate that the pointer<br />

isn’t currently pointing to anything. The <strong>Objective</strong>-C convention is to use nil when<br />

referring to an object and relegating NULL for use with older C-style data types.<br />

Initializing the pointer to one of these special values enables an if (y != nil)<br />

check to determine if the pointer is currently pointing to anything. Because nil is<br />

equivalent to the value 0, you may also see this condition written as if (!y).<br />

Also be careful not to dereference a NULL pointer. Trying to read or write from a<br />

pointer that points to nothing causes an access violation error, which immediately<br />

exits your application.<br />

3.4 Communicating with objects<br />

In most C-inspired languages such as C++, Java, and C#, developers call a method<br />

implemented by an object. Developers using <strong>Objective</strong>-C, on the other hand, don’t<br />

“call” a method directly; instead, they “send” a message to an object. The object<br />

“receives” the message and decides if it wants to process it, usually by invoking a<br />

method with the same name. This is a fundamentally different approach and is one of<br />

the many features that make <strong>Objective</strong>-C a more dynamic language because it enables<br />

the object to have finer-grained control over how method dispatch occurs.<br />

3.4.1 Sending a message to an object<br />

Figure 3.3 outlines the basic <strong>Objective</strong>-C syntax for sending a message to an object. In<br />

source code, a message send is represented by a set of square brackets; immediately<br />

after the opening bracket is the target, followed by the name of the message itself. The<br />

target can be any expression that evaluates to an object that should receive the message.<br />

You can consider a basic message send such as the one<br />

shown in figure 3.3 as analogous to addressing an envelope<br />

with a person’s name and address. It sets up a container<br />

that can then be delivered to its intended recipient.<br />

Sometimes, for an object to make sense of a message,<br />

additional information must be sent along with it, similar<br />

to placing a letter or invoice in an envelope. Additional<br />

information provided in a message send is represented by<br />

one or more values called arguments. When sending a<br />

message, arguments are provided by placing a colon character<br />

after the method name, as shown in figure 3.4.<br />

Figure 3.3 The basic syntax<br />

for sending a message to an<br />

object in <strong>Objective</strong>-C. In a pair<br />

of square brackets, the target<br />

(or object that receives the<br />

message) is specified,<br />

followed by the name of the<br />

message itself.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!