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

Page 205

Advertising
background image

Commonly Used Dictionary Methods

205

– removeObjectForKey:

Removes the entry for a specified key.

– removeObjectsForKeys:

Removes the entries for each key in a specified array.

– setDictionary:

Removes all the entries in the receiver, then adds the entries from
a specified dictionary.

Representing Dictionaries as Strings

– description

Returns a string that represents the contents of the receiver. For
example, the following code excerpt produces the string
“{“seatAssignment” = “Window”; “section” = “Non-smoking”;
“aircraft” = “747”}”:

id preferences = [NSMutableDictionary

dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "section", "aircraft")];

id description = [preferences description];

Storing Dictionaries

– writeToFile:atomically:

Writes the dictionary’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. It does not create a new file at the specified
path unless the write is successful. The resulting file is suitable
for use with

dictionaryWithContentsOfFile:

. For example, the following

excerpt creates an NSMutableDictionary from the contents of the
file specified by

path

, updates the object for the key @“Language”,

and saves the updated dictionary back to the same file:

id defaults = [NSMutableDictionary

dictionaryWithContentsOfFile:path];

[defaults setObject:newLanguagePreference forKey:@"Language"];
[defaults writeToFile:path atomically:YES];

See also

description

.

Advertising