Class

NSMutableDictionary

A dynamic collection of objects associated with unique keys.

Declaration

@interface NSMutableDictionary<__covariant KeyType, __covariant ObjectType> : NSDictionary

Overview

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.

NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionaryRef. See Toll-Free Bridging for more information on toll-free bridging.

Setting Values Using Subscripting

In addition to the provided instance methods, such as setObject:forKey:, you can access NSDictionary values by their keys using subscripting.

id value = @"someValue";
mutableDictionary[@"someKey"] = value;

Subclassing Notes

There should typically be little need to subclass NSMutableDictionary. If you do need to customize behavior, it is often better to consider composition rather than subclassing.

Methods to Override

In a subclass, you must override both of its primitive methods:

You must also override the primitive methods of the NSDictionary class.

Topics

Creating and Initializing a Mutable Dictionary

+ dictionaryWithCapacity:

Creates and returns a mutable dictionary, initially giving it enough allocated memory to hold a given number of entries.

- initWithCapacity:

Initializes a newly allocated mutable dictionary, allocating enough memory to hold numItems entries.

- init

Initializes a newly allocated mutable dictionary.

+ dictionaryWithSharedKeySet:

Creates a mutable dictionary which is optimized for dealing with a known set of keys.

Adding Entries to a Mutable Dictionary

- setObject:forKey:

Adds a given key-value pair to the dictionary.

- setObject:forKeyedSubscript:

Adds a given key-value pair to the dictionary.

- setValue:forKey:

Adds a given key-value pair to the dictionary.

- addEntriesFromDictionary:

Adds to the receiving dictionary the entries from another dictionary.

- setDictionary:

Sets the contents of the receiving dictionary to entries in a given dictionary.

Removing Entries From a Mutable Dictionary

- removeObjectForKey:

Removes a given key and its associated value from the dictionary.

- removeAllObjects

Empties the dictionary of its entries.

- removeObjectsForKeys:

Removes from the dictionary entries specified by elements in a given array.

Relationships

Inherits From

See Also

Basic Collections

NSArray

A static ordered collection of objects.

NSMutableArray

A dynamic ordered collection of objects.

NSDictionary

A static collection of objects associated with unique keys.

NSSet

A static unordered collection of unique objects.

NSMutableSet

A dynamic unordered collection of unique objects.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software