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.

484<br />

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

Booleans<br />

In C, there is no Boolean type. In operations, zero is considered false, while anything other than<br />

zero is considered true. Objective-C maintains this convention but adds the scalar type BOOL,<br />

whose values are YES and NO.<br />

Equality<br />

Both C and Objective-C use the = operator for assignment and the == operator to represent<br />

equality. However, since objects in Objective-C are represented by pointers, comparing two<br />

objects with == will not return true unless both are the same pointer.<br />

To work around this problem, NSObject defines the isEqual: method, which compares the<br />

objects’ hash values. Although it’s possible to override isEqual:, it’s not easy, because you also<br />

have to override hash, and hashing is tricky. Plus, it’s often much faster to disqualify equality<br />

based on something like the length of a string or the number of objects in an array than it is to<br />

generate a hash. As such, many classes have their own equality methods, as shown in Table 26-2.<br />

Table 26-2. Class-Specific Equality Methods<br />

Class Method<br />

NSArray isEqualToArray:<br />

NSData isEqualToData:<br />

NSDate isEqualToDate:<br />

NSDictionary isEqualToDictionary:<br />

NSHashTable isEqualToHashTable:<br />

NSIndexSet isEqualToIndexSet:<br />

NSNumber isEqualToNumber:<br />

NSSet isEqualToSet:<br />

NSValue isEqualToValue:<br />

It’s not as bad as sounds. Aside from the rather obvious pattern, Xcode’s autocomplete is<br />

there to help. Whenever you type isEqual, instead of typing the colon and the argument, press the<br />

Esc key. This will open the autocomplete list, and you can see whether a more specific completion<br />

is available.<br />

Void<br />

CAUTION Don’t be fooled by isEqualTo:, which is part of AppleScript and should not be<br />

used directly.<br />

C functions can return nothing by declaring themselves to be of type void and can accept any<br />

pointer by declaring arguments that are pointers to void:<br />

void nothingFromSomething(void *anything)<br />

Void return types and void pointers can also be used in Objective-C:<br />

- (void)nothingFromSomething:(void *)anything;

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

Saved successfully!

Ooh no, something went wrong!