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.

Here it is again, in slow motion:<br />

NSObject *object; // Declaration<br />

object = // Assignment<br />

[NSObject alloc]; // Allocation<br />

[object init]; // Initialization<br />

CAUTION This is just an example. Apple recommends you never call alloc and init on two<br />

different lines.<br />

Once an object is instantiated, memory management gets a bit more complicated, so we’ll<br />

have to come back to it. The traditional memory management scheme is discussed in the<br />

“Objective-C Memory Management” section. The new garbage collection API is discussed in<br />

the “Objective-C 2.0” section.<br />

Scalars<br />

In C, scalars are width specific, but in general, you want to use scalars with the same width as the<br />

architecture for which it is compiled. <strong>Leopard</strong> introduces width-agnostic scalars that resolve into<br />

the architecture’s native width, as shown in Table 26-1.<br />

Table 26-1. Width-Agnostic Versions of C Scalars<br />

<strong>Leopard</strong> C Description<br />

NSInteger int, long Signed integer<br />

NSUInteger unsigned int, unsigned long Unsigned integer<br />

CGFloat float, double Floating-point number<br />

Logging<br />

C’s printf and friends have no concept of Objective-C objects, other than that they are pointers.<br />

The function NSLog has all the tokens and formatting features of printf but adds the %@ token,<br />

which calls any object’s description method.<br />

Strings<br />

In C, a string is just an array of char terminated with NULL. In Objective-C, you should use the<br />

NSString class. As an object, NSString provides several built-in methods and a mutable subclass,<br />

as well as handling things such as Unicode. NSString constants can be declared inline with a compiler<br />

directive in the form @"This is a string".<br />

Arrays<br />

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

C arrays are just blocks of memory with the convenience of a bracket operator and pointer arithmetic.<br />

There is, for example, absolutely no way of knowing how long an array is, given just the<br />

array itself. In Objective-C, use NSArray, which, like NSString, has convenient methods and a<br />

mutable subclass. Beyond this, NSArray uses behind-the-scenes optimizations to use the data<br />

structure most efficient for the length and operations required.

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

Saved successfully!

Ooh no, something went wrong!