Sorting arrays, Adding and removing objects – Apple WebObjects 3.5 User Manual

Page 198

Advertising
background image

Chapter 11

WebScript Programmer’s Quick Reference to Foundation Classes

198

– isEqual:

Returns YES if the specified object is an array and has contents
equivalent to the receiver; NO, otherwise. Two arrays have equal
contents if they each hold the same number of objects and objects at a
given index in each array satisfy the

isEqual:

test.

– objectAtIndex:

Returns the object located at a specified index. Arrays have a zero-
based index. The first object in an array is at index 0, the second is at
index 1, and so on. It is an error to specify an index that is out of bounds
(greater than or equal to the array’s count).

– indexOfObject:

Returns the index of the first object in the array that is equivalent to a
specified object. To determine equality, each element of the array is
sent an

isEqual:

message.

– indexOfObjectIdenticalTo:

Returns the index of the first occurrence of a specified object. To
determine equality, the

id

s of the two objects are compared.

Sorting Arrays

– sortedArrayUsingSelector:

Returns an NSArray that lists the receiver’s elements in ascending
order, as determined by a specified method. This method is used to
sort arrays containing strings and/or numbers. For example, the
following code excerpt creates the NSArray

sortedArray

containing the

string “Alice” at index 0, “David” at index 1, and so on:

id guestArray = @("Suzy", "Alice", "John", "Peggy", "David");
id sortedArray = [guestArray sortedArrayUsingSelector:@"compare:"];

Adding and Removing Objects

Warning:

The following methods are not supported by NSArray. They are

available only to NSMutableArray objects.

– addObject:

Adds a specified object at the end of the receiver. It is an error to
specify

nil

as an argument to this method. You cannot add

nil

to an array.

Advertising