Name

NSDictionary — Mac OS X 10.0

Synopsis

This class manages a collection of objects as key-value pairs: objects are identified by a unique key within the dictionary. A key-value pair within a dictionary is called an entry. NSDictionary is an immutable class whose contents cannot be altered after they have been initially set. If you need a mutable dictionary, use NSMutableDictionary instead. A dictionary key is typically an NSString, but according to the API the key can be any object that is type id. The fact that a key can be an object of any type opens the way for interesting design possibilities in Cocoa.

NSDictionary has three primitive methods upon which the rest of the API is based. They are count, objectForKey:, and keyEnumerator. The count method returns the number of objects contained in the dictionary. The method objectForKey: is used to access an object in the dictionary. Finally, keyEnumerator will return an NSEnumerator object that will enumerate the keys of the dictionary. In addition to keyEnumerator, NSDictionary responds to objectEnumerator for enumerating the contents of the dictionary. Note that there is no order in a dictionary as there is in an array. When enumerating the contents of a dictionary, there is no guarantee regarding the order that member objects will be returned by the enumerator.

Often we want to send a message to each member of a collection. NSDictionary provides a method that save us from the burden of having to enumerate the contents of the dictionary ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.