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.

166 CHAPTER 8 Dynamic typing and runtime type information<br />

8.2 Dynamic binding<br />

<strong>Objective</strong>-C, like Smalltalk, can use dynamic typing; an object can be sent a message<br />

that isn’t specified in its interface. Flexibility is increased because an object can “capture”<br />

a message and pass the message along to a different object that can respond to<br />

the message appropriately. This behavior is known as message forwarding or delegation.<br />

Alternatively, an error handler can be used in case the message can’t be forwarded. If<br />

an object doesn’t forward a message, respond to it, or handle an error, the message is<br />

silently discarded.<br />

Statically typed objects are still dynamically bound<br />

Messages sent to statically typed objects are dynamically bound, just as objects<br />

typed id are. The exact type of a statically typed receiver is still determined at<br />

runtime as part of the messaging process. Here is a display message sent to<br />

thisObject:<br />

Rectangle *thisObject = [[Square alloc] init];<br />

[thisObject display];<br />

This code performs the version of the method defined in the Square class, not the<br />

one in its Rectangle superclass.<br />

8.3 How messaging works<br />

A lot of introductory texts about <strong>Objective</strong>-C make a big deal that messages are sent to<br />

objects rather than methods being invoked on an object, but what does this really mean,<br />

and how does the distinction affect how you go about developing your applications?<br />

If an <strong>Objective</strong>-C compiler sees a message expression such as the following<br />

[myRentalProperty setAddress:@"13 Adamson Crescent"];<br />

it doesn’t determine which method in the CTRentalProperty class implements the<br />

setAddress: message and hardcode a call to it. Instead, the <strong>Objective</strong>-C compiler<br />

converts the message expression into a call to a messaging function called<br />

objc_msgSend. This C-style function takes the receiver (object to receive the message)<br />

and the name of the message (the method selector) as its first two arguments followed,<br />

conceptually at least, by the rest of the arguments specified in the message<br />

send, as follows:<br />

objc_msgSend(<br />

myRentalProperty,<br />

@selector(setAddress:),<br />

@"13 Adamson Crescent"<br />

);<br />

The message send function does everything required to implement dynamic binding. It<br />

uses the message name and receiver object to determine the correct method implementation<br />

to invoke and then calls it before returning the value as its own return value.

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

Saved successfully!

Ooh no, something went wrong!