Next: Creating a NSDictionary Up: NSDictionary Previous: NSDictionary

What is it

An instance of NSDictionary contains a set of keys, and for each key, an associate value. Both keys and values must be objects; the keys must all be different from each other, and must not be nil; the values must not be nil.

An example of NSDictionary may be represented as follows:

{
  Luca = "/opt/picture.png";
  "Birthday Photo" = "/home/nico/birthday.png";
  "Birthday Image" = "/home/nico/birthday.png";
  "My Sister" = "/home/marghe/pic.jpg";
}
In this example, for each key (Luca, etc), which is a string (an image name), there is associated a value (/opt/picture.png), which is a string (the full path of the file containing the image). Note that different keys may have the same values. In this case, the same actual graphic file can be accessed using two different names.

In this example, all the objects were strings, but that not need be always the case; keys and values may be arbitrary objects (and not necessarily of the same class). A basic difference between NSArray and NSDictionary is that the elements contained in an NSArray are lined up in a precise order, while the couples key/value contained in a NSDictionary are not ordered at all. You usually access an object in an array by specifying its position in the array (its index); in a dictionary instead, you rather ask for the value associated with a certain key. So, while arrays are useful to maintain an ordered list of objects, dictionaries are useful to maintain mappings between certain keys and certain values. In other contexts, dictionaries are called hash tables.


Next: Creating a NSDictionary Up: NSDictionary Previous: NSDictionary
Nicola 2002-01-13