Creating arrays, Querying arrays – Apple WebObjects 3.5 User Manual

Page 197

background image

Commonly Used Array Methods

197

Creating Arrays

The methods in this section are class methods, as denoted by the plus sign
(+). You use class methods to send messages to a class—in this case,
NSArray or NSMutableArray. For more information on class methods, see
“Sending a Message to a Class” (page 172).

+ array

Returns an empty array. Usually used to create NSMutableArrays.
NSArrays created with this method are permanently empty.

// Most common use
id mutableArray = [NSMutableArray array];

// May not be what you want
id array = [NSArray array];

+ arrayWithObject:

Returns an array containing the single specified object.

+ arrayWithObjects:

Returns an array containing the objects in the argument list. The
argument list is a comma-separated list of objects ending with

nil

.

id array = [NSMutableArray arrayWithObjects:

@"Plates", @"Plasticware", @"Napkins", nil];

+ arrayWithArray:

Returns an array containing the contents of a specified array.
Usually used to create an NSMutableArray from an immutable
NSArray. For example, the following statement creates an
NSMutableArray from a constant NSArray object:

id mutableArray = [NSMutableArray

arrayWithArray:@("A", "B", "C")];

+ arrayWithContentsOfFile:

Returns an array initialized from the contents of a specified file.
The specified file can be a full or relative pathname; the file that it
names must contain a string representation of an array, such as that
produced by the

writeToFile:atomically:

method.

See also

description

(page 200) .

Querying Arrays

– count

Returns the number of objects in the array.