NSDictionary and NSMutableDictionary in Objective-C
Last Updated :
28 Apr, 2025
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
// 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.
- 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
// 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; }
{}
Similar Reads
NSArray and NSMutableArray in Objective-C An array is a data structure that allows to store data in contiguous memory. They can be used to store primitive data types such as int, float, double, char, etc of any particular type or can store derived data types such as structures, pointers, etc. Its indexing starts with 0 i.e. the first elemen
5 min read
NSSet and NSMutableSet in Objective-C NSSet and NSMutableSet are two classes in Objective-C that are used for representing collections of objects. Both classes provide an ordered set of unique objects, with the difference being that NSSet is immutable, while NSMutableSet is mutable, which means its contents can be changed dynamically. I
3 min read
Data Encapsulation in Objective-C Encapsulation is an Object-Oriented Programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Encapsulation in Objective-C is vital for pro
6 min read
Constants in Objective-C Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
Inheritance in Objective-C In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
Memory Management in Objective-C Memory management is a core concept irrespective of any programming language. The process of allocation of memory of objects when needed and deallocation when they are no longer in use is called Memory Management. Memory is a limited resource and so has to be dealt with carefully. If unrequired memo
7 min read
Numbers in Objective-C Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Fast Enumeration in Objective-C Fast Enumeration is basically defined as the concept of a collection of data types that contains a fixed set of constants. So they can be used at various places in the programming language. As we know that Objective - C is similar to other programming languages so it supports all the features that w
4 min read
Literals in Objective-C Literals are the values that are assigned to a variable and which remain constant over the whole program. Literals are the values that cannot be modified in the program after declaration. Literals values are taken the same as variable values but their values cannot be altered in a program during its
6 min read
Dates and Times in Objective-C In Objective-C, dates and times are represented using the NSDate and NSDateFormatter classes. NSDate class represents a single point in time. It stores an absolute point in time, independent of any particular calendar or time zone. You can use NSDate to represent a date and time, or just a date or t
4 min read