Have you ever wanted a simple way to store and retrieve your data objects?
Most programming languages have a facility to management lists of data that is indexed by keys. That is – you can store an object in a list and assign a key that could be used in the future to retrieve the data. This eliminates the need to manually move through long lists of objects to find what you are looking for.
In order to visualize this, imagine the typical Objective-C array that you create from the NSArray (or NSMutableArray) class. This array would look something like this:
To find something in this list you probably need to go through the entire list and do some kind of check for each object:
Is this object three? No.
Is this object three? No.
Is this object three. Yes!
That is fine, but it would be easier if you could simply use an indexed data source that would look something like this:
Then, all you need to do is say is this: get me the object at index 3.
As long as you know what index is needed then you only need one step to get your information. The benefit may not be as obvious in the simple example using strings, but as you start to store complex objects in these data structures you will find that using dictionaries will simplify your coding life.
Without further ado – here is how you do this in Objective-C. We are doing to use the NSMutableArray class to create a dictionary.
That is all there is too it – it is relatively painless to use a dictionary. Check out the documentation on NSDictionary to see the other built-in goodies that come with using this class.
What situations do you think you would rather use a dictionary instead of a normal array?