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.

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

You called a method named aSimpleDynamicMethod<br />

You called a method named aSimpleDynamicMethod<br />

You called a method named aSimpleDynamicMethod<br />

You called a method named aSimpleDynamicMethod<br />

The five messages that sent the aSimpleDynamicMethod message to the CTRental-<br />

Property object all correctly invoked the aSimpleDynamicMethod function, as evidenced<br />

by the results of the NSLog call it contains.<br />

An interesting point to notice is the first NSLog message, indicating that a method<br />

named aSimpleDynamicMethod was added to the CTRentalProperty class. This<br />

method results because the first time the house object receives the aSimpleDynamic-<br />

Method message, it detects that the object doesn’t respond to this selector. Therefore,<br />

the <strong>Objective</strong>-C runtime automatically invokes the object’s resolveInstanceMethod:<br />

method to see if it’s possible to correct this problem. In the implementation of<br />

resolveInstanceMethod:, the <strong>Objective</strong>-C runtime function class_addMethod<br />

dynamically adds a method to the class and returns YES, indicating that you’ve<br />

resolved the missing method. The four future message sends, which send the<br />

aSimpleDynamicMethod selector, don’t all produce equivalent log messages, because<br />

when these copies of the message are received, the <strong>Objective</strong>-C runtime finds that the<br />

object does have a method implementation for the aSimpleDynamicMethod selector,<br />

and hence it doesn’t need to dynamically attempt to resolve the situation.<br />

Forwarding methods and dynamic method resolution are largely orthogonal. A<br />

class has the opportunity to dynamically resolve a method before the forwarding<br />

mechanism kicks in. If resolveInstanceMethod: returns YES, message forwarding<br />

isn’t actioned.<br />

The dangerous practice of method swizzling<br />

Because you can add new methods at runtime, you might be interested to learn that<br />

you can also replace an existing method with a new implementation at runtime, even<br />

without access to source code for the class in question.<br />

The <strong>Objective</strong>-C runtime function method_exchangeImplementations accepts two<br />

methods and swaps them, so that calling one causes the implementation of the other<br />

one to be invoked.<br />

This technique is commonly called method swizzling, but it’s a particularly dangerous<br />

practice that usually indicates you’re doing something wrong or something not<br />

intended by the original creators of the object in question. It’s dangerous because it<br />

relies on so many undocumented or brittle aspects of a class’s implementation.<br />

8.5 Practical uses of runtime type introspection<br />

The iOS development platform has matured with multiple versions of the iPhone<br />

operating system and variants for the iPad all exhibiting slightly different capabilities<br />

and restrictions.

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

Saved successfully!

Ooh no, something went wrong!