Table of Contents
Previous Section
// Most common use id mutableDictionary = [NSMutableDictionary dictionary]; // May not be what you want id dictionary = [NSDictionary dictionary];
id preferences = [NSMutableDictionary
dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "smoking", "aircraft")];
id customerPreferences = [NSDictionary dictionaryWithObjectsAndKeys: seatingPreference, @"seatAssignment", smokingPreference, @"smoking", aircraftPreference, @"aircraft", nil];
id index;
id keys = [dictionary allKeys];
for (index = 0; index < [keys count]; index++) {
value = [dictionary objectForKey:[keys objectAtIndex:index];
// Use the value
}
creates the NSArray keys and uses it to access the value of each entry in the dictionary.
id choices = @{"Steak" = 3; "Seafood" = 2; "Pasta" = 1};
id keys = [choices sortedByValueUsingSelector:@"compare:"];
creates the NSArray keys containing the string "Pasta" at index 0, "Seafood" at index 1, and "Steak" at index 2.
id preferences = [NSMutableDictionary
dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "section", "aircraft")];
id sectionPreference = [dictionary objectForKey:@"section"];
produces the NSString sectionPreference with the contents "non-smoking".
id dictionary = [NSMutableDictionary dictionaryWithDictionary:
@{"seatAssignment" = "window"}];
[dictionary setObject:@"non-smoking" forKey:@"section"];
[dictionary setObject:@"aisle" forKey:@"seatAssignment"];
produces the NSMutableDictionary dictionary with the value "non-smoking" for the key "section" and the value "aisle" for the key "seatAssignment." Notice that the original value for "seatAssignment" is replaced.
id preferences = [NSMutableDictionary
dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "section", "aircraft")];
id description = [preferences description];
produces the string "{"seatAssignment" = "Window"; "section" = "Non-smoking"; "aircraft" = "747"}".
id defaults = [NSMutableDictionary dictionaryWithContentsOfFile:path]; [defaults setObject:newLanguagePreference forKey:@"Language"]; [defaults writeToFile:path atomically:YES];
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.
Table of Contents
Next Section