Cocoa Programming for Mac OS X - Aaron Hillegass [17]
Figure 3.4. Inheritance Diagram
Let’s go through a few of the commonly used methods on these classes. For a complete listing, you can access the online documentation in Xcode’s Help menu.
NSObject
NSObject is the root of the entire Objective-C class hierarchy. Some commonly used methods on NSObject are described next.
- (id)init
Initializes the receiver after memory for it has been allocated. An init message is generally coupled with an alloc message in the same line of code:
TheClass *newObject = [[TheClass alloc] init];
- (NSString *)description
Returns an NSString that describes the receiver. The debugger’s print object command (“po”) invokes this method. A good description method will often make debugging easier. Also, if you use %@ in a format string, the object that should be substituted in is sent the message description. The value returned by the description method is put into the log string. For example, the line in your main function
NSLog(@"The number at index %d is %@", i, numberToPrint);
is equivalent to
NSLog(@"The number at index %d is %@", i,
[numberToPrint description]);
- (BOOL)isEqual:(id)anObject
Returns YES if the receiver and anObject are equal and NO otherwise. You might use it like this:
if ([myObject isEqual:anotherObject]) {
NSLog(@"They are equal.");
}
But what does equal really mean? In NSObject, this method is defined to return YES if and only if the receiver and anObject are the same object—that is, if both are pointers to the same memory location.
Clearly, this is not always the “equal” that you would hope for, so this method is overridden by many classes to implement a more appropriate idea of equality. For example, NSString overrides the method to compare the characters in the receiver and anObject. If the two strings have the same characters in the same order, they are considered equal.
Thus, if x and y are NSStrings, there is a big difference between these two expressions:
x == y
and
[x isEqual:y]
The first expression compares the two pointers. The second expression compares the characters in the strings. Note, however, that if x and y are instances of a class that has not overridden NSObject’s isEqual: method, the two expressions are equivalent.
NSArray
An NSArray is a list of pointers to other objects. It is indexed by integers. Thus, if there are n objects in the array, the objects are indexed by the integers 0 through n – 1. You cannot put a nil in an NSArray. (This means that there are no “holes” in an NSArray, which may confuse some programmers who are used to Java’s Object[].) NSArray inherits from NSObject.
An NSArray is created with all the objects that will ever be in it. You can neither add nor remove objects from an instance of NSArray. We say that NSArray is immutable. (Its mutable subclass, NSMutableArray, will be discussed next.) Immutability is nice in some cases. Because it is immutable, a horde of objects can share one NSArray without worrying that one object in the horde might change it. NSString and NSNumber are also immutable. Instead of changing a string or number, you will simply create another one with the new value. (In the case of NSString, there is also the class NSMutableString that allows its instances to be altered.)
A single array can hold objects of many different classes. Arrays cannot, however, hold C primitive types, such as int or float.
Here are some commonly used methods implemented by NSArray:
- (unsigned)count
Returns the number of objects currently in the array.
- (id)objectAtIndex:(unsigned)i
Returns the object located at index i. If i is beyond the end of the array, you will get an error at runtime.
- (id)lastObject
Returns the object in the array with the highest index value. If the array is empty, nil is returned.
- (BOOL)containsObject:(id)anObject