01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

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.

How messaging works<br />

167<br />

Messages are slower than function calls<br />

Message sending is somewhat slower than calling a function directly. Typically this<br />

overhead is insignificant compared to the amount of work performed in the method.<br />

The overhead is also reduced by some internal tricks in the implementation of<br />

objc_msgSend, which manages to reduce the amount of repetitive work performed in<br />

scenarios such as a message being sent to all objects in an array.<br />

In the rare case that method sending causes an undue performance hit, it’s possible<br />

to bypass several aspects of <strong>Objective</strong>-C’s dynamism. Bypassing should be performed,<br />

however, only after analysis using tools such as Instruments.<br />

Behind the scenes, an <strong>Objective</strong>-C method ends up being transformed and compiled<br />

as a straight C function, designed to be invoked via objc_msgSend. A method like this<br />

- (void)setAddress:(NSString *)newAddress { ... }<br />

can be imagined to be translated into a C function similar to the following:<br />

void CTRentalProperty_setAddress(<br />

CTRentalProperty *self,<br />

SEL _cmd,<br />

NSString *str<br />

)<br />

{<br />

...<br />

}<br />

The C-style function isn’t given a name because it can’t be accessed directly, so it isn’t<br />

made visible to the linker. The C function expects two arguments in addition to those<br />

explicitly declared in the <strong>Objective</strong>-C method declaration. We previously discussed the<br />

self parameter, which is implicitly sent in a message send to indicate the object that<br />

received the message; the lesser-known implicit parameter is named _cmd. This parameter<br />

holds the selector (and hence name) of the message that was sent in order to<br />

invoke this method implementation. Later in this chapter we discuss a couple of techniques<br />

whereby a single method implementation can in fact respond to more than<br />

one unique method selector.<br />

8.3.1 Methods, selectors, and implementations<br />

In the previous section, a number of new concepts were introduced: method, selector,<br />

implementation, message, and message send. What exactly are these terms, and how<br />

do they relate to source code?<br />

■<br />

■<br />

■<br />

Method—A piece of code associated with a class and given a particular name,<br />

such as (void)setAddress:(NSString *)newValue { ... }.<br />

Selector—An efficient way to present the name of a message at runtime. Represented<br />

by the SEL data type.<br />

Implementation—A pointer to the machine code that implements a method.<br />

Represented by the IMP data type.

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

Saved successfully!

Ooh no, something went wrong!