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
}
id choices = @{"Steak" = 3; "Seafood" = 2; "Pasta" = 1};
id keys = [choices sortedByValueUsingSelector:@"compare:"];
id preferences = [NSMutableDictionary
dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "section", "aircraft")];
id sectionPreference = [dictionary objectForKey:@"section"];
id dictionary = [NSMutableDictionary dictionaryWithDictionary:
@{"seatAssignment" = "window"}];
[dictionary setObject:@"non-smoking" forKey:@"section"];
[dictionary setObject:@"aisle" forKey:@"seatAssignment"];
- It is an error to specify nil as an argument for setObject: or forKey:. You can't put nil in a dictionary as a key or as a value.
id preferences = [NSMutableDictionary
dictionaryWithObjects:@("window", "non-smoking", "747")
forKeys:@("seatAssignment", "section", "aircraft")];
id description = [preferences description];
id defaults = [NSMutableDictionary dictionaryWithContentsOfFile:path]; [defaults setObject:newLanguagePreference forKey:@"Language"]; [defaults writeToFile:path atomically:YES];- See also description.
Table of Contents
Next Section