Graphics Programs Reference
In-Depth Information
someObject = [someArray objectAtIndex:0];
A dictionary's objects are not ordered within the collection. So instead of accessing
entries with an index, you use a
key
. The key is usually an instance of
NSString
.
// Add some object to a dictionary for the key "MyKey"
[someDictionary setObject:someObject forKey:@"MyKey"];
// Get that same object out
someObject = [someDictionary objectForKey:@"MyKey"];
We call each entry in a dictionary a
key-value pair
. The
value
is the object being stored in
the collection, and the
key
is a unique value (usually a string) that you use to retrieve the
value later. (In other development environments, a dictionary is called a
hash map
or
hash
table
, but we still use the term key-value pair to talk about the information they store.)
Figure 12.10 NSDictionary diagram
The purpose of a dictionary is difficult to understand if you are new to programming, but
the general idea is that a dictionary maps one value to another (
Figure 12.10
)
. Consider a
robot that responded to the facial expressions of a human. For example, if a human
smiled, the robot would wink. Thus, the human-to-robot response would be stored in a
dictionary. The “wink” response would be the value stored for the “smile” key. When a
human made another facial expression at the robot, it would look up that expression in its
dictionary and find the appropriate response.