05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

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.

Magic, right? A few lines bear closer examination. First, the functions for directly interacting<br />

with the runtime are not part of Cocoa, so they have to be imported separately:<br />

#import <br />

To facilitate just this sort of goings on, the runtime sends the load message to any class as it’s<br />

being loaded but before it’s used. This is the perfect place to accomplish what you need to do:<br />

+ (void)load;<br />

To swap the implementations of the methods, you have to get the methods:<br />

Method oldMethod = class_getInstanceMethod(self, @selector(description));<br />

The compiler directive @selector converts the name of a method into a SEL, as required by<br />

the function class_getInstanceMethod, which returns a Method. The Method is a struct that contains<br />

a selector of type SEL, an implementation of type IMP, and some return type encoding information.<br />

With the methods in hand, all you have to do is swap their implementations.<br />

method_exchangeImplementations(oldMethod, newMethod);<br />

As far as the runtime is concerned, the two methods have literally had their implementations<br />

swapped. The message description will now call the implementation you defined for<br />

prettyDescription. The inverse is also true; calling prettyDescription will return the original<br />

implementation of description. So, to “call super” from within the new implementation, it has to<br />

call itself.<br />

- (NSString *)prettyDescription;<br />

{<br />

NSString *oldDescription = [self prettyDescription];<br />

...<br />

}<br />

Weird, right?<br />

In Objective-C, nothing is ever written in stone. Even without wandering far from the comforts<br />

of object-oriented programming, you have the power to pull off staggering feats of<br />

engineering brilliance.<br />

Key Value Coding<br />

In recent years, the designers of Objective-C have begun making use of heretofore informal<br />

aspects of the language. For example, given a property key, you can assume its getter is key and<br />

its setter is setKey:. Alternately, if key is a Boolean property, you can assume its getter is isKey. By<br />

making this assumption about the predictability of method names, the system can enable a new<br />

level of dynamic behavior known as key value coding (KVC).<br />

KVC treats every property as a named key and introduces a generic form for getting or setting<br />

any property by name:<br />

id property = [myObject valueForKey:@"property"];<br />

[otherObject setValue:property forKey:@"property"];<br />

CHAPTER 26 MAC <strong>OS</strong> X DEVELOPMENT: OBJECTIVE-C 503<br />

By allowing you to refer to properties by name, KVC lets you do the following:<br />

• Resolve property names at runtime.<br />

• Deal with unknown or empty keys on the fly.<br />

• Store keys in collections and deal with them en masse without regard to type.

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

Saved successfully!

Ooh no, something went wrong!