Cocoa Programming for Mac OS X - Aaron Hillegass [18]
Returns YES if anObject is present in the array. This method determines whether an object is present in the array by sending an isEqual: message to each of the array’s objects and passing anObject as the parameter.
- (unsigned)indexOfObject:(id)anObject
Searches the receiver for anObject and returns the lowest index whose corresponding array value is equal to anObject. Objects are considered equal if isEqual: returns YES. If none of the objects in the array are equal to anObject, indexOfObject: returns NSNotFound.
NSMutableArray
NSMutableArray inherits from NSArray but extends it with the ability to add and remove objects. To create a mutable array from an immutable one, use NSArray’s mutableCopy method.
Here are some commonly used methods implemented by NSMutableArray:
- (void)addObject:(id)anObject
Inserts anObject at the end of the receiver. You are not allowed to add nil to the array.
- (void)addObjectsFromArray:(NSArray *)otherArray
Adds the objects contained in otherArray to the end of the receiver’s array of objects.
- (void)insertObject:(id)anObject atIndex:(unsigned)index
Inserts anObject into the receiver at index, which cannot be greater than the number of elements in the array. If index is already occupied, the objects at index and beyond are shifted up one slot to make room. You will get an error if anObject is nil or if index is greater than the number of elements in the array.
- (void)removeAllObjects
Empties the receiver of all its elements.
- (void)removeObject:(id)anObject
Removes all occurrences of anObject in the array. Matches are determined on the basis of anObject’s response to the isEqual: message.
- (void)removeObjectAtIndex:(unsigned)index
Removes the object at index and moves all elements beyond index down one slot to fill the gap. You will get an error if index is beyond the end of the array.
As mentioned earlier, you cannot add nil to an array. Sometimes, you will want to put an object into an array to represent nothingness. The NSNull class exists for exactly this purpose. There is exactly one instance of NSNull, so if you want to put a placeholder for nothing into an array, use NSNull like this:
[myArray addObject:[NSNull null]];
NSString
An NSString is a buffer of Unicode characters. In Cocoa, all manipulations involving character strings are done with NSString. As a convenience, the Objective-C language also supports the @"..." construct to create a string object constant from a 7-bit ASCII encoding:
NSString *temp = @"this is a constant string";
NSString inherits from NSObject. Here are some commonly used methods implemented by NSString:
- (id)initWithFormat:(NSString *)format, ...
Works like sprintf. Here, format is a string containing tokens, such as %d. The additional arguments are substituted for the tokens:
int x = 5;
char *y = "abc";
id z = @"123";
NSString *aString = [[NSString alloc] initWithFormat:
@"The int %d, the C String %s, and the NSString %@",
x, y, z];
- (NSUInteger)length
Returns the number of characters in the receiver.
- (NSString *)stringByAppendingString:(NSString *)aString
Returns a string object made by appending aString to the receiver. The following code snippet, for example, would produce the string “Error: unable to read file.”
NSString *errorTag = @"Error: ";
NSString *errorString = @"unable to read file.";
NSString *errorMessage;
errorMessage = [errorTag stringByAppendingString:errorString];
- (NSComparisonResult)compare:(NSString *)otherString
Compares the receiver and otherString and returns NSOrderedAscending if the receiver is alphabetically prior to otherString, NSOrderedDescending if otherString is comes before the receiver, or NSOrderedSame if the receiver and otherString are equal.
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)
otherString
Like compare:, except the comparison ignores letter case.
“Inherits from” versus “Uses” or “Knows About”
Beginning Cocoa programmers