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.

482<br />

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

hue, and, uh . . . needless to say, most programmers spend a lot of time looking up functions to<br />

remember how to write them, let alone ever trying to read them. Now, consider the same thing<br />

as an Objective-C method:<br />

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

It’s pretty clear what each of the arguments mean, whether you’re writing the code for the<br />

first time or you’re reading someone else’s code three years down the line.<br />

A method’s prototype is equally readable, outlining not only the meaning of the method<br />

parameters but also their types, the return type, and whether the method belongs to the class or<br />

its individual instances:<br />

- (void)setColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue<br />

alpha:(CGFloat)alpha;<br />

This method is of type void, so it returns nothing. All its arguments are of type CGFloat. As<br />

detailed in this chapter, CGFloat is a width-neutral floating-point scalar that is converted at compile<br />

time into a float on 32-bit systems or a double on 64-bit systems. In this case, the method is<br />

an instance method, as indicated by the starting -. Class methods start with +.<br />

This method is, for all intents and purposes, identical to the following function:<br />

void setColor(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha);<br />

As would be expected, pointers to objects are represented with the * operator. For example,<br />

a class method that took an argument of type NSNumber and returned an NSString would look<br />

like this:<br />

+ (NSString *)stringWithNumber:(NSNumber *)number;<br />

Objective-C Improves C<br />

Although C has its own compiler directives, using the # prefix, Objective-C introduces a new set<br />

of its own, using the @ prefix. This is an artifact of Objective-C’s origins as a C preprocessor but<br />

continues to serve as a convenient sigil of which features are part of Objective-C and which features<br />

are just plain C.<br />

Objective-C separates interface from implementation but does so at the preprocessor level<br />

with the directives @interface, @implementation, and @end. This allows the use of separate header<br />

and implementation files but also allows multiple classes to be defined in a single file, as appropriate.<br />

Finally, many parts of the C standard library have new, upgraded versions in Cocoa that<br />

should be used instead. The older C features are still there, and the two can be intermixed.<br />

Memory Management<br />

Object life cycles and interaction complicate memory management, so the entire C memory system<br />

has been abstracted. Although malloc and free are still there, they’re not used. Instantiation<br />

in Objective-C is actually a four-step process: declaration, assignment, allocation, and initialization.<br />

These are usually done on one line:<br />

NSObject *object = [[NSObject alloc] init];

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

Saved successfully!

Ooh no, something went wrong!