Basics Of Objective-C And Dictionaries (NSDictionary)
July 9, 2012
Dictionaries are very common in iOS application development. A dictionary is nothing more than a collection of key-value pairs. The keys are represented as strings and the values are objects. The keys in a dictionary must be unique. As with other collections, dictionaries have two variants, mutable and non-mutable.
Let’s look at a few examples. The code below creates a mutable dictionary and adds a series of objects, including a string, a number and an array (with various embedded object types):
NSArray *array = [NSArray arrayWithObjects: @"String 1", [NSNumber numberWithInteger:99], [NSArray arrayWithObjects: @"array value 1", @"array value 2", nil], nil]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:@"String value" forKey:@"stringKey"]; [dictionary setObject:[NSNumber numberWithInt:25] forKey:@"numberKey"]; [dictionary setObject:array forKey:@"arrayKey"]; NSLog(@"dictionary: %@", dictionary); |
The output for this dictionary follows:
dictionary: {
arrayKey = (
"String 1",
99,
(
"array value 1",
"array value 2"
)
);
numberKey = 25;
stringKey = "String value";
} |
To get the values from a dictionary, use the method objectForKey:
NSLog(@"Value for arrayKey: %@", [dictionary objectForKey:@"arrayKey"]); NSLog(@"Value for stringKey: %@", [dictionary objectForKey:@"stringKey"]); |
The output for the two keys are as follows:
Value for arrayKey: (
"String 1",
99,
(
"array value 1",
"array value 2"
)
)
Value for stringKey: String value |
Here is a more involved example, where I create an array of dictionaries. I define the keys as strings – also, notice that I call dictionaryWithObjectsAndKeys to create a dictionary from a series of key-value pairs, versus the approach above where key-value pairs are added one by one. Each dictionary is added to a mutable array:
// Define keys NSString *style = @"Style"; NSString *appearance = @"Appearance"; NSString *hopProfile = @"Bitterness"; // A dictionary object NSDictionary *dict; // Create array to hold dictionaries NSMutableArray *arrayOfDictionaries = [NSMutableArray array]; // Create three dictionaries dict = [NSDictionary dictionaryWithObjectsAndKeys: // Object and key pairs @"Bock", style, @"Deep Gold", appearance, [NSNumber numberWithInt:25], hopProfile, nil]; [arrayOfDictionaries addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: // Object and key pairs @"India Pale Ale (IPA)", style, @"Copper", appearance, [NSNumber numberWithInt:50], hopProfile, nil]; [arrayOfDictionaries addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"Stout", style, @"Jet Black", appearance, [NSNumber numberWithInt:35], hopProfile, nil]; [arrayOfDictionaries addObject:dict]; NSLog(@"array of dictionaries: %@", arrayOfDictionaries); |
I find it helpful to see the output of a dictionary to get a visual of the contents. The NSLog output is shown below:
array of dictionaries: (
{
Appearance = "Deep Gold";
Bitterness = 25;
Style = Bock;
},
{
Appearance = Copper;
Bitterness = 50;
Style = "India Pale Ale (IPA)";
},
{
Appearance = "Jet Black";
Bitterness = 35;
Style = Stout;
}
) |



