13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

30CHAPTER 2: Object-Oriented ProgrammingSending MessagesAs shown in Listing 2-3, <strong>Objective</strong>-C message-send operations are enclosed within squarebraces. The target of the message, the receiver, is placed to the left, and the message,interspersed with its arguments, is placed to the right. This is on one level similar to the systemused in other object-oriented languages such as C++, Ruby, or Java, and slightly different dueto the inline placement of arguments within the message name. Figure 2-2 shows how these twoparadigms map to one another, with <strong>Objective</strong>-C’s messaging syntax on top, and the C-stylemethod calls below.Figure 2-2. Messaging (top) vs. method-calling (bottom)Unlike function names in C-style languages, the <strong>Objective</strong>-C message name is broken up with itsarguments interspersed, each preceded by a colon character. It is important to note that no subpartsof an <strong>Objective</strong>-C message name are optional, nor can their order be changed. While somelanguages implement a concept known as named parameters or keyword parameters, whichmight look similar, those concepts suggest that certain arguments can be reordered or omittedat runtime and that some arguments might have predefined default values. None of thesecharacteristics apply to <strong>Objective</strong>-C messages: the placement of arguments within the messagename is simply provided as a syntactic aid to the programmer where long lists of parametersmight be required. Consider Listing 2-4, where similar functions are called using <strong>Objective</strong>-Cand C++.Listing 2-4. Contrasting Method Invocations[myObject drawLineFromX: 100 Y: 120 toX: 140 Y: 120 weight: 2 color: redColor];myObject.drawLine(100, 120, 140, 120, 2, redColor);In the <strong>Objective</strong>-C example, the meaning of each parameter is immediately clear. Reading theC++ example, however, this isn’t as clear without visiting a header file or documentation page toread up on the method’s arguments.The unusual syntax of <strong>Objective</strong>-C’s messages do not preclude anything you might be usedto from other languages, however: the results of functions or other messages can be placeddirectly inline as arguments or receivers. The latter is typified by the [[MyObject alloc] init]sequence; the former could be any result, nested to any level, as in Listing 2-5. Note that theindenting is only for formatting purposes: <strong>Objective</strong>-C, like C, effectively treats all groups ofwhitespace the same as a single space.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!