Open In App

NSDictionary and NSMutableDictionary in Objective-C

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Dictionary in Objective-C stores objects in a key-value pair. The data items stored in a dictionary can be accessed using its key. It is dynamic in nature means the size of the dictionary grows according to the need. In Dictionary, the key cannot be null, but the value can be NULL. It can be mutable or immutable.

Mutability means changeable i.e. if something is mutable we can change that thing. In an array or string or dictionary, mutability means that we can change an array element or a string’s character or a dictionary’s entry can be changed during runtime. But if an array or string or dictionary is immutable, we can’t change an array element or a string’s character or a dictionary’s entry during runtime.

NSDictionary

NSDictionary in Objective-C stores an immutable dictionary of objects. Since it is also a kind of dictionary, it stores data in key-value pairs where its values are accessed by its keys. Since it is immutable, we can’t change dictionary items during runtime, but we only replace the existing dictionary. It can hold NSNumber, NSString, etc, and their mutable objects.

Some Important methods of NS Dictionary

  1. dictionaryWithObjects: forKeys:  -> It creates a dictionary and initializes it with objects and their keys
  2. dictionaryWithObjectsAndKeys: -> It creates a dictionary with entities in the form of key:value.
  3. count -> It returns the total number of entries in the dictionary.
  4. allKeys -> It returns an array of all the keys present in the dictionary.
  5. allValues -> It returns an array of all the values present in the dictionary.
  6. valueForKey: -> It returns the value of the object at the given key (used for accessing dictionary entry).

Example:

In the below given example, we are initializing the NSDictionary named dictionary1 with one key-value pair i.e. key1-gfg. Also, we are initializing another dictionary named dictionary2 with three key-value pairs. Then we are applying the above-given methods to count the number of entries, access the value of the given key, etc.

ObjectiveC




// Objective-C program to demonstrate NSDictionary
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    // Initialising with dictionaryWithObjects: forKeys:
    NSDictionary *dictionary1 =  [NSDictionary dictionaryWithObject:@"gfg" forKey:@"key1"];  
  
    // Printing dictionary1
    NSLog(@"%@ \n", dictionary1);   
     
    // Initialising with dictionaryWithObjectsAndKeys:
    NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"geeks", @"key1", @"for", @"key2", @"gfgwebsite", @"key3", nil];
 
    // Printing dictionary2
    NSLog(@"%@ \n", dictionary2); 
     
     
    // Count
    NSLog(@"Number of entries in Dictionary 2 is : %d \n", [dictionary2 count]);
     
    // All keys
    NSLog(@"All keys in Dictionary 2 is : %@ \n", [dictionary2 allKeys]);
     
    // All values
    NSLog(@"All values in Dictionary 2 is : %@ \n", [dictionary2 allValues]);
     
    // Value for key i.e. accessing dictionary entry
    NSLog(@"Value for key3 in Dictionary 2 is : %@ \n", [dictionary2 valueForKey:@"key3"]);
     
    [pool drain];
    return 0;
}


Output

{key1 = gfg; } 
{key1 = geeks; key2 = for; key3 = gfgwebsite; } 
Number of entries in Dictionary 2 is : 3
All keys in Dictionary 2 is : (key3, key2, key1)
All values in Dictionary 2 is : (gfgwebsite, for, geeks)
Value for key3 in Dictionary 2 is : gfgwebsite

NSMutableDictionary

NSMutableDictionary in Objective-C stores a mutable dictionary of objects. It also stores data items in the same way as NS Dictionary stores. But it comes with more advantages than NSDictionary. Since it is mutable, we can change dictionary items during runtime i.e adding new key-value pairs, replacing them, removing them, etc.

Some Important methods of NSMutableDictionary

Since the NSMutableDictionary is inherited from NSDictionary, all the instance methods of the NSDictionary can be used in NS MutableDictionary. In addition to these methods, we also use the below-mentioned methods.

  1. setValue: forKey: -> inserts the given key-value pair into the dictionary.
  2. removeObjectForKey: -> removes the given key-value pair from the dictionary.
  3. removeAllObjects -> Deletes all the entries from the dictionary, hence making it empty.

Example:

In the below given example, we are initializing an empty NSMutableDictionary named mutableDictionary. Then we are applying the above-given methods to count the number of entries, remove an entry, remove all entries, access the value of the given key, etc.

ObjectiveC




// Objective-C program to demonstrate NSMutableDictionary
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSMutableDictionary *mutableDictionary =  [[NSMutableDictionary alloc] init];
  
    // Set value for key i.e adding an entry
    [mutableDictionary setValue:@"gfg" forKey:@"Key1"];
    [mutableDictionary setValue:@"geeks" forKey:@"Key2"];
    [mutableDictionary setValue:@"gfgwebsite" forKey:@"Key3"];
     
    // Printing mutableDictionary
    NSLog(@"%@ \n",mutableDictionary); 
     
    // Count
    NSLog(@"Number of entries in Mutable Dictionary is : %d \n", [mutableDictionary count]);
     
    // All keys
    NSLog(@"All keys in Mutable Dictionary is : %@ \n", [mutableDictionary allKeys]);
     
    // All values
    NSLog(@"All values in Mutable Dictionary is : %@ \n", [mutableDictionary allValues]);
     
    // Value for key i.e. accessing dictionary entry
    NSLog(@"Value for Key3 in Mutable Dictionary is : %@ \n", [mutableDictionary valueForKey:@"Key3"]);
     
    // removeObject for key
    [mutableDictionary removeObjectForKey:@"key2"];
     
    // Printing mutableDictionary
    NSLog(@"%@ \n",mutableDictionary);
     
    // Remove All objects
    [mutableDictionary removeAllObjects];
     
    // Printing mutableDictionary
    NSLog(@"%@ \n",mutableDictionary); // Will print empty dictionary
     
    [pool drain];
    return 0;
}


Output

{Key1 = gfg; Key2 = geeks; Key3 = gfgwebsite; } 
Number of entries in Mutable Dictionary is : 3 
All keys in Mutable Dictionary is : (Key3, Key1, Key2) 
All values in Mutable Dictionary is : (gfgwebsite, gfg, geeks) 
Value for Key3 in Mutable Dictionary is : gfgwebsite 
{Key1 = gfg; Key2 = geeks; Key3 = gfgwebsite; } 
{}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads