Objective-C Language NSDictionary Creating using dictionaryWithObjectsAndKeys:


NSDictionary *inventory = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:13], @"Mercedes-Benz SLK250",
    [NSNumber numberWithInt:22], @"Mercedes-Benz E350",
    [NSNumber numberWithInt:19], @"BMW M3 Coupe",
    [NSNumber numberWithInt:16], @"BMW X6",

nil must be passed as the last parameter as a sentinel signifying the end.

It's important to remember that when instantiating dictionaries this way the values go first and the keys second. In the example above the strings are the keys and the numbers are the values. The method's name reflects this too: dictionaryWithObjectsAndKeys. While this is not incorrect, the more modern way of instantiating dictionaries (with literals) is prefered.