Show Menu

Need an iOS Developer?

Submit your 30 day Job Listing for FREE

Have you had a collection of objects that you want to by name? Lets say you had an object that contains contact info for a person. If I wanted to see the contact information for somebody, I’d want to just tell something, “Hey, give me Jimmy’s contact info”, and it would get it for me. That is exactly what NSDictionary does for you.

NSDictionary is way to perform easy lookups for objects stored in the dictionary, via a key of some sort. It basically stores data in something called a “key value pair”. I will use the even simpler example of looking up the definition of a word in a standard Miriam-Webster style dictionary. The word you are looking up is the “key.” The definition you are trying to find for that word is the “value.” In this post, I’ll go over some of the basics.

Creating a NSDictionary

There are many ways to create it, but I will go over some of the simpler ones here. The first one I learned is making a NSDictionary from another NSDictionary. You might be saying to yourself, “How is that useful when you’re learning? Making a NSDictionary from another one means I need one in the first place!” And you would be right, so that is what Apple gives you!

NSDictionary *wordDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"A friendly mammal commonly kept as a pet", @"Cat",
@"An awesome smartphone", @"iPhone",
@"An informative and helpful blog about Objective-C", @"", nil];

Shameless self plug aside, that is a very easy way to make an initial dictionary. You just tell the NSDictionary class to make one based off of the keys you give it. There are a few VERY important things to know about how to use this though.

The order is Value, then Key — While my example makes it obvious, you must put the Value object first, then its associated key. One reference I found had this backwards, which had me quite frustrated when it didn’t work. I figured it out eventually be giving my dictionary the command [wordDictionary allKeys], which returns an NSArray of all keys, and found it was telling me the equivalent of the definitions in this example, instead of the words.

It must be terminated with nil — As far as my research has shown me, that nil at the end just says that the list of objects is complete.

A similar method is:

dictionaryWithObjects:(NSArray *)values forKeys:(NSArray *)keys

This one does the same thing, but in this case you have two NSArrays. One filled with all values, the other with all keys, then it makes a dictionary by stepping through those.

Update 11/11/13: I have recently learned from the Stanford iOS classes of a newer way to create an NSDictionary that is much simpler. It’s syntax is as follows:

NSDictionary *someDictionary = @{key1 : value1, key2 :value2, …};

So this one has the order I like where it is key then value, and it is a LOT less verbose. Also, this one does not need to be nil terminated. If you wanted just 2 things, you would just put the end curly brace where that last comma is. If you wanted more ,just repeat the pattern. Similar to how when Apple made it easy to make an NSString from a C string with the @ sign, they use it here to provide a shortcut.

Reading from a Dictionary

Now that we have something stored, how do we get it out? While there are plenty of ways, this is the way I am currently using:

NSString *definitionOfiPhone = [wordDictionary objectForKey:@"iPhone"];

This way is pretty self explanatory, that it sends a message to the wordDictionary requesting an object associated with the key, which in this case is an NSString of “iPhone.”

Something important to note, the objects and keys can be just about anything that inherits from NSObject, which is almost anything you’ll deal with except for C primitives like int, double, etc. For the sake of simplicity and explanation, I used an NSString for both, but you could have them as NSNumbers, or your own custom class. The only thing they can’t be is “nil”, but you could use NSNull as a placeholder.

Update 11/11/13: As with my above note about an easier way to make an NSDictionary, I also learned about a much easier way to ready from one. This way is much similar to how I read them in a dictionary in C#, so it feels a lot more familiar, and again, is a lot less verbose.

NSString *definitionOfiPhone = wordDictionary[@"iPhone"];

You just basically use they key as if it were an index in an array.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

Nick Hanan is learning Objective-C and wants to help others on the same journey. He started the Coding Explorer Blog to chronicle what he is learning as he explores the Objective-C landscape. Check out his blog for information from a beginners perspective