NSDictionary and NSMutableDictionary in Objective-C
Last Updated :
12 Dec, 2022
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
- dictionaryWithObjects: forKeys: -> It creates a dictionary and initializes it with objects and their keys
- dictionaryWithObjectsAndKeys: -> It creates a dictionary with entities in the form of key:value.
- count -> It returns the total number of entries in the dictionary.
- allKeys -> It returns an array of all the keys present in the dictionary.
- allValues -> It returns an array of all the values present in the dictionary.
- 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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSDictionary *dictionary1 = [ NSDictionary dictionaryWithObject: @"gfg" forKey: @"key1" ];
NSLog ( @"%@ \n" , dictionary1);
NSDictionary *dictionary2 = [ NSDictionary dictionaryWithObjectsAndKeys:
@"geeks" , @"key1" , @"for" , @"key2" , @"gfgwebsite" , @"key3" , nil ];
NSLog ( @"%@ \n" , dictionary2);
NSLog ( @"Number of entries in Dictionary 2 is : %d \n" , [dictionary2 count]);
NSLog ( @"All keys in Dictionary 2 is : %@ \n" , [dictionary2 allKeys]);
NSLog ( @"All values in Dictionary 2 is : %@ \n" , [dictionary2 allValues]);
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.
- setValue: forKey: -> inserts the given key-value pair into the dictionary.
- removeObjectForKey: -> removes the given key-value pair from the dictionary.
- 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
#import <Foundation/Foundation.h>
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSMutableDictionary *mutableDictionary = [[ NSMutableDictionary alloc] init];
[mutableDictionary setValue: @"gfg" forKey: @"Key1" ];
[mutableDictionary setValue: @"geeks" forKey: @"Key2" ];
[mutableDictionary setValue: @"gfgwebsite" forKey: @"Key3" ];
NSLog ( @"%@ \n" ,mutableDictionary);
NSLog ( @"Number of entries in Mutable Dictionary is : %d \n" , [mutableDictionary count]);
NSLog ( @"All keys in Mutable Dictionary is : %@ \n" , [mutableDictionary allKeys]);
NSLog ( @"All values in Mutable Dictionary is : %@ \n" , [mutableDictionary allValues]);
NSLog ( @"Value for Key3 in Mutable Dictionary is : %@ \n" , [mutableDictionary valueForKey: @"Key3" ]);
[mutableDictionary removeObjectForKey: @"key2" ];
NSLog ( @"%@ \n" ,mutableDictionary);
[mutableDictionary removeAllObjects];
NSLog ( @"%@ \n" ,mutableDictionary);
[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; }
{}
Share your thoughts in the comments
Please Login to comment...