NSMutableDictionary Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSDictionary.h NSKeyValueCoding.h |
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.
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.
Tasks
Creating and Initializing a Mutable Dictionary
Adding Entries to a Mutable Dictionary
-
– setObject:forKey: -
– setObject:forKeyedSubscript: -
– setValue:forKey: -
– addEntriesFromDictionary: -
– setDictionary:
Removing Entries From a Mutable Dictionary
Class Methods
dictionaryWithCapacity:
Creates and returns a mutable dictionary, initially giving it enough allocated memory to hold a given number of entries.
Parameters
- numItems
The initial capacity of the new dictionary.
Return Value
A new mutable dictionary with enough allocated memory to hold numItems entries.
Discussion
Mutable dictionaries allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.
Availability
- Available in OS X v10.0 and later.
See Also
-
dictionary(NSDictionary) -
dictionaryWithContentsOfFile:(NSDictionary) -
dictionaryWithContentsOfURL:: (NSDictionary) -
dictionaryWithObject:forKey:(NSDictionary) -
dictionaryWithObjects:forKeys:: (NSDictionary) -
dictionaryWithObjects:forKeys:count:(NSDictionary) -
dictionaryWithObjectsAndKeys:(NSDictionary) -
– initWithCapacity:
Declared In
NSDictionary.hdictionaryWithSharedKeySet:
Creates a mutable dictionary which is optimized for dealing with a known set of keys.
Parameters
- keyset
The keyset, created by the
NSDictionaryclass methodsharedKeySetForKeys:. If keyset isnil, an exception is thrown. If keyset is not an object returned bysharedKeySetForKeys:, an exception is thrown.
Return Value
A new mutable dictionary optimized for a known set of keys.
Discussion
Keys that are not in the key set can still be set in the dictionary, but that usage is not optimal.
Availability
- Available in OS X v10.8 and later.
Declared In
NSDictionary.hInstance Methods
addEntriesFromDictionary:
Adds to the receiving dictionary the entries from another dictionary.
Parameters
- otherDictionary
The dictionary from which to add entries
Discussion
Each value object from otherDictionary is sent a retain message before being added to the receiving dictionary. In contrast, each key object is copied (using copyWithZone:—keys must conform to the NSCopying protocol), and the copy is added to the receiving dictionary.
If both dictionaries contain the same key, the receiving dictionary’s previous value object for that key is sent a release message, and the new value object takes its place.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSDictionary.hinitWithCapacity:
Initializes a newly allocated mutable dictionary, allocating enough memory to hold numItems entries.
Parameters
- numItems
The initial capacity of the initialized dictionary.
Return Value
An initialized mutable dictionary, which might be different than the original receiver.
Discussion
Mutable dictionaries allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSDictionary.hremoveAllObjects
Empties the dictionary of its entries.
Discussion
Each key and corresponding value object is sent a release message.
Availability
- Available in OS X v10.0 and later.
Declared In
NSDictionary.hremoveObjectForKey:
Removes a given key and its associated value from the dictionary.
Parameters
- aKey
The key to remove.
Discussion
Does nothing if aKey does not exist.
For example, assume you have an archived dictionary that records the call letters and associated frequencies of radio stations. To remove an entry for a defunct station, you could write code similar to the following:
NSMutableDictionary *stations = nil; |
stations = [[NSMutableDictionary alloc] |
initWithContentsOfFile: pathToArchive]; |
[stations removeObjectForKey:@"KIKT"]; |
Availability
- Available in OS X v10.0 and later.
Declared In
NSDictionary.hremoveObjectsForKeys:
Removes from the dictionary entries specified by elements in a given array.
Parameters
- keyArray
An array of objects specifying the keys to remove.
Discussion
If a key in keyArray does not exist, the entry is ignored.
Availability
- Available in OS X v10.0 and later.
Declared In
NSDictionary.hsetDictionary:
Sets the contents of the receiving dictionary to entries in a given dictionary.
Parameters
- otherDictionary
A dictionary containing the new entries.
Discussion
All entries are removed from the receiving dictionary (with removeAllObjects), then each entry from otherDictionary added into the receiving dictionary.
Availability
- Available in OS X v10.0 and later.
Declared In
NSDictionary.hsetObject:forKey:
Adds a given key-value pair to the dictionary.
Parameters
- anObject
The value for aKey. A strong reference to the object is maintained by the dictionary. Raises an
NSInvalidArgumentExceptionif anObject isnil. If you need to represent anilvalue in the dictionary, useNSNull.- aKey
The key for value. The key is copied (using
copyWithZone:; keys must conform to theNSCopyingprotocol). Raises anNSInvalidArgumentExceptionif aKey is nil. If aKey already exists in the dictionary anObject takes its place.
Availability
- Available in OS X v10.0 and later.
Declared In
NSDictionary.hsetObject:forKeyedSubscript:
Adds a given key-value pair to the dictionary.
Parameters
- object
The value for aKey. A strong reference to the object is maintained by the dictionary. Raises an
NSInvalidArgumentExceptionif anObject isnil. If you need to represent anilvalue in the dictionary, useNSNull.- aKey
The key for value. The key is copied (using
copyWithZone:; keys must conform to theNSCopyingprotocol). Raises anNSInvalidArgumentExceptionif aKey is nil. If aKey already exists in the dictionary anObject takes its place.
Discussion
This method is identical to setObject:forKey:.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSDictionary.hsetValue:forKey:
Adds a given key-value pair to the dictionary.
Parameters
- value
The value for key.
- key
The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).
Discussion
This method adds value and key to the dictionary using setObject:forKey:, unless value is nil in which case the method instead attempts to remove key using removeObjectForKey:.
Availability
- Available in OS X v10.3 and later.
See Also
-
valueForKey:(NSDictionary)
Declared In
NSKeyValueCoding.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-07-17)