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.

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

■<br />

Messages are dynamically bound to methods. A runtime procedure matches the<br />

method selector in a message with a particular method implementation that<br />

“belongs to” the receiver object. The mapping can change or be modified<br />

dynamically to change the behavior of the application and to introduce aspects<br />

such as logging without recompilation.<br />

These features give object-oriented programs a great deal of flexibility and power, but<br />

there’s a price to pay. To permit better compile-time type checking and to make code<br />

more self-documenting, you can also choose to more statically type your use of objects<br />

in <strong>Objective</strong>-C.<br />

8.1 Static vs. dynamic typing<br />

If you declare a variable with a particular object type, the variable can only hold an<br />

object of the specified type or one of its subclasses. The <strong>Objective</strong>-C compiler also<br />

issues warning if, from the static type information available to it, it appears that an<br />

object assigned to that variable won’t be able to respond to a particular message. As<br />

an example, considering the following code snippet:<br />

CTRentalProperty *house = ...;<br />

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

[house setFlag:YES];<br />

Because the variable is statically typed to be of type CTRentalProperty, the compiler<br />

can verify that it should respond to the setAddress: message. On checking the<br />

CTRentalProperty class, however, the compiler will issue a warning that the class may<br />

not respond to the setFlag: message, because according to the static type information<br />

available to the compiler, the CTRentalProperty class doesn’t provide such a message.<br />

Static type checks such as the one demonstrated here can be a helpful<br />

development aid because they enable errors such as typos or incorrect capitalization<br />

to be detected at compile time rather than runtime.<br />

Static typing contrasts with the dynamic typing available when the id datatype is<br />

utilized, as in the following code sample:<br />

id house = ...;<br />

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

[house setFlag:YES];<br />

Statically typed objects have the same internal data structure as objects declared to be<br />

ids. The type doesn’t affect the object. It affects only the amount of information given<br />

to the compiler about the object and the amount of information available to those<br />

reading the source code.<br />

8.1.1 Making assumptions about the runtime type<br />

It’s possible to have your cake and eat it too. By this we mean that you can have some<br />

of the benefits of the flexibility associated with dynamic typing while having greater<br />

compile-time checks approaching the level of those offered by fully static typing.

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

Saved successfully!

Ooh no, something went wrong!