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.

498<br />

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

In object-oriented languages such as Java and C++, once classes are compiled, they cease to<br />

be dynamic. You can subclass them and do what you like with your subclasses, but you can’t<br />

change them. Your programming environment is never fully dynamic, because all any programmer<br />

can do is create and play in a dynamic sandbox surrounding a static core.<br />

Objective-C doesn’t have any such restrictions. Remember that Objective-C is only a very<br />

thin layer on top of C. Except for a few compiler directives, notable by their @ prefix, very little<br />

above plain C is actually occurring at compile time. With Objective-C, the magic happens at runtime.<br />

That means much of what the code is doing is not locked down until right before it<br />

happens, and therein lies tremendous power.<br />

At the core of this is Objective-C’s unusual messaging syntax. It’s not just a readable replacement<br />

for dot syntax; it’s actually an indirection provided by the runtime that completely<br />

separates the act of calling a method from the actual execution of that method.<br />

Consider the previous example:<br />

[myObject setColorWithRed:0.4 green:0.3 blue:0.0 alpha:1.0];<br />

In the parlance of Objective-C, we call myObject the receiver and setColorWithRed:0.4<br />

green:0.3 blue:0.0 alpha:1.0 the message. Behind the scenes, the runtime separates the message<br />

into a method signature and an array of arguments.<br />

Every method signature is resolved into a selector, which is represented by the data type SEL.<br />

The runtime dynamically pairs selectors with a method implementation, represented by the data<br />

type IMP. These are tangible data types and can be treated as such. They can be created, examined,<br />

changed, and passed around.<br />

NOTE A method’s name is the method declaration, including the colons, but without the<br />

argument names or types. So, the name of the method being called in setColorWithRed:0.4<br />

green:0.3 blue:0.0 alpha:1.0 is setColorWithRed:green:blue:alpha:.<br />

Categories<br />

Even if it were possible for the designers of a library to consider every possible method programmers<br />

might need, it would still be a bad idea. Imagine how hard it would be to use a basic class<br />

such as NSString if it implemented every possible method. Even if you managed to page through<br />

the thousands of available methods to find the one you need, performance would be awful.<br />

Object-oriented programming gives you the ability to extend existing classes by subclassing<br />

them and adding the functionality you need. For example, NSString provides a method that takes<br />

a string and appends it to the receiver.<br />

NSString *greeting = @"Hello";<br />

greeting = [greeting stringByAppendingString:@", world!"];<br />

NSLog(greeting);<br />

Hello, world!<br />

What if you wanted to prepend a string? That is, append it before the receiver? You can’t;<br />

there’s no such method. However, you could subclass NSString, creating BMString:<br />

#import <br />

@interface BMString : NSString<br />

- (NSString *)stringByPrependingString:(NSString *)aString;<br />

@end

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

Saved successfully!

Ooh no, something went wrong!