<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSMutableDictionary",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableDictionary"
  },
  "title" : "NSMutableDictionary"
}
-->

# NSMutableDictionary

A dynamic collection of objects associated with unique keys.

```
class NSMutableDictionary
```

## Overview

In Swift, you can use this type instead of a <doc://com.apple.documentation/documentation/Swift/Dictionary> variable in cases that require reference semantics.

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`](/documentation/Foundation/NSDictionary).

`NSMutableDictionary` is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFMutableDictionary>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

### Setting Values Using Subscripting

In addition to the provided instance methods, such as [`setObject(_:forKey:)`](/documentation/Foundation/NSMutableDictionary/setObject(_:forKey:)), you can access `NSDictionary` values by their keys using *subscripting*.

```swift
let 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:

- [`setObject(_:forKey:)`](/documentation/Foundation/NSMutableDictionary/setObject(_:forKey:))
- [`removeObject(forKey:)`](/documentation/Foundation/NSMutableDictionary/removeObject(forKey:))

You must also override the primitive methods of the [`NSDictionary`](/documentation/Foundation/NSDictionary) class.

## Topics

### Creating and Initializing a Mutable Dictionary

[`dictionaryWithCapacity:`](/documentation/Foundation/NSMutableDictionary/dictionaryWithCapacity:)

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

[`init(capacity:)`](/documentation/Foundation/NSMutableDictionary/init(capacity:))

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

[`init()`](/documentation/Foundation/NSMutableDictionary/init())

Initializes a newly allocated mutable dictionary.

[`init(sharedKeySet:)`](/documentation/Foundation/NSMutableDictionary/init(sharedKeySet:))

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

### Adding Entries to a Mutable Dictionary

[`setObject(_:forKey:)`](/documentation/Foundation/NSMutableDictionary/setObject(_:forKey:))

Adds a given key-value pair to the dictionary.

[`setObject:forKeyedSubscript:`](/documentation/Foundation/NSMutableDictionary/setObject:forKeyedSubscript:)

Adds a given key-value pair to the dictionary.

[`setValue(_:forKey:)`](/documentation/Foundation/NSMutableDictionary/setValue(_:forKey:))

Adds a given key-value pair to the dictionary.

[`addEntries(from:)`](/documentation/Foundation/NSMutableDictionary/addEntries(from:))

Adds to the receiving dictionary the entries from another dictionary.

[`setDictionary(_:)`](/documentation/Foundation/NSMutableDictionary/setDictionary(_:))

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

### Removing Entries From a Mutable Dictionary

[`removeObject(forKey:)`](/documentation/Foundation/NSMutableDictionary/removeObject(forKey:))

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

[`removeAllObjects()`](/documentation/Foundation/NSMutableDictionary/removeAllObjects())

Empties the dictionary of its entries.

[`removeObjects(forKeys:)`](/documentation/Foundation/NSMutableDictionary/removeObjects(forKeys:))

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)