Working with dictionaries, Storing arrays, Representing arrays as strings – Apple WebObjects 3.5 User Manual

Page 200

Advertising
background image

Chapter 11

WebScript Programmer’s Quick Reference to Foundation Classes

200

Storing Arrays

– writeToFile:atomically:

Writes the array’s string representation to a specified file using the

description

method. Returns YES on success and NO on failure. If YES

is specified for

atomically:

, this method attempts to write the file safely

so that an existing file with the specified path is not overwritten, and
it does not create a new file at the specified path unless the write is
successful. The resulting file is suitable for use with

arrayWithContentsOfFile:

. For example, the following code excerpt creates

guestArray

with the contents of the specified file, adds a new guest, and

saves the changes to the same file:

id guestArray = [NSMutableArray arrayWithContentsOfFile:path];
[guestArray addObject:newGuest];
[guestArray writeToFile:path atomically:YES];

Representing Arrays as Strings

– description

Returns a string that represents the contents of the receiver. For
example, the following code excerpt produces the string “(Plates,
Plasticware, Napkins)”:

id array = [NSMutableArray arrayWithObjects:

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

id description = [array description];

– componentsJoinedByString:

Returns an NSString created by interposing a specified string between
the elements of the receiver’s objects. Each element of the array must
be a string. If the receiver has no elements, an empty string is returned.
See also

componentsSeparatedByString:

(page 193). For example, the

following code excerpt creates the NSString

dashString

with the contents

“A-B-C”:

id commaString = @"A, B, C";
id array = [string componentsSeparatedByString:@","];
id dashString = [array componentsJoinedByString:@"-"];

Working With Dictionaries

NSDictionary and NSMutableDictionary objects store collections of key-value
pairs. The key-value pairs within a dictionary are called entries. Each entry
consists of an object that represents the key, and a second object that represents

Advertising