Previously we learn how to define String in Objective C: Objective-C : Strings


The NSDictionary class represents an unordered collection of objects; however, they associate each value with a key, which acts like a label for the value. This is useful for modeling relationships between pairs of objects. In NSDictionary, Entries are stored in the form of key value pairs. A Dictionary can hold NSNumber, NSString, NSArray, ..etc and their mutable objects.

NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.)


A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:). In general, a key can be any object, but note that when using key-value coding the key must be a string. Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.



The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary.


CFDictionary Reference

CFDictionary and its derived mutable type, CFMutableDictionary Reference, manage associations of key-value pairs. CFDictionary creates static dictionaries where you set the key-value pairs when first creating a dictionary and cannot modify them afterward; CFMutableDictionary creates dynamic dictionaries where you can add or delete key-value pairs at any time, and the dictionary automatically allocates memory as needed.


Creating Dictionary Objects

An empty, immutable dictionary object may be created as follows:

NSDictionary * bookListing = [NSDictionary dictionary];


Similarly, an empty mutable dictionary may be created as follows:

NSMutableDictionary * bookListing = [NSMutableDictionary dictionary];


Initializing and Adding Entries to a Dictionary Object

Each key-value pair contained within a dictionary is referred to as an entry. Once a relationship between a key and a value has been established that relationship cannot subsequently be modified.

New entries are added to a dictionary using the setObject instance method. This method takes as its arguments an object and a corresponding key:

#import <Foundation/Foundation.h>

int main ()
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    NSMutableDictionary *colorListing = [NSMutableDictionary dictionary];
    [colorListing setObject: @"Red"  forKey: @"1111"];
    [colorListing setObject: @"Green" forKey:  @"2222"];
    [colorListing setObject: @"Blue" forKey:  @"3333"];
    [colorListing setObject: @"Yellow" forKey: @"4444"];
    [pool drain];
   return 0;

When executed, the above code will be displayed:

2015-07-10 07:40:34.452 demo[25953] {1111 = Red; 2222 = Green; 3333 = Blue; 4444 = Yellow; }

In the above example, the colorListing dictionary is initialized with four color names with corresponding reference codes to act as keys.


It is also possible to create and initialize a dictionary with a number of key-value pairs using the dictionaryWithObjectsAndKeys class method. For example, an alternative to the above code is as follows:

NSDictionary *colorListing = [NSDictionary dictionaryWithObjectsAndKeys: @"Red", @"1111", @"Green", @"2222", @"Blue", @"3333", @"Yellow", @"4444", nil];


Dictionaries may also be initialized using keys and values contained in arrays using the arrayWithObjects method:

NSArray *objectsArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
NSArray *keysArray = [NSArray arrayWithObjects: @"1111", @"2222", @"3333", @"4444", nil];
NSDictionary *colorListing = [[NSDictionary alloc] initWithObjects: objectsArray forKeys: keysArray];

A count of the number of entries in a dictionary can be obtained using the count instance methods:

int count;
NSLog (@"Number of books in dictionary = %lu", [bookListing count]);

Dictionary entries are accessed by referencing the key corresponding to the required entry via the objectForKey method. For example:

NSLog ( @"2222 = %@", [colorListing objectForKey: @"2222"]);


Comparing Dictionaries

isEqualToDictionary method is used to compare dictionaries. It returns YES, if two dictionaries are equal.
#import <Foundation/Foundation.h>
int main()
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    NSMutableDictionary * dict1 = [NSMutableDictionary
    NSMutableDictionary * dict2 = [NSMutableDictionary
    NSMutableDictionary * dict3 = [NSMutableDictionary
    if([dict1 isEqualToDictionary:dict2])
        NSLog(@" dict 1 and dict 2 are equal");
    if(![dict1 isEqualToDictionary:dict3])
        NSLog(@" dict 1 and dict 3 are not equal");

    [pool drain];
   return 0;

Next, we will learn about: Objective-C : NSAutoreleasePool  class

  Modified On Nov-19-2017 01:58:26 PM

Leave Comment