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

# NSMapTable

A collection similar to a dictionary, but with a broader range of available memory semantics.

```
class NSMapTable<KeyType, ObjectType> where KeyType : AnyObject, ObjectType : AnyObject
```

## Overview

The map table is modeled after [`NSDictionary`](/documentation/Foundation/NSDictionary) with the following differences:

- Keys and/or values are optionally held “weakly” such that entries are removed when one of the objects is reclaimed.
- Its keys or values may be copied on input or may use pointer identity for equality and hashing.
- It can contain arbitrary pointers (its contents are not constrained to being objects).

You can configure an [`NSMapTable`](/documentation/Foundation/NSMapTable) instance to operate on arbitrary pointers and not just objects, although typically you are encouraged to use the C function API for void * pointers. The object-based API (such as [`setObject(_:forKey:)`](/documentation/Foundation/NSMapTable/setObject(_:forKey:))) will not work for non-object pointers without type-casting.

When configuring map tables, note that only the options listed in [`NSMapTableOptions`](/documentation/Foundation/NSMapTableOptions) guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration. While other [`NSPointerFunctions`](/documentation/Foundation/NSPointerFunctions) options are used for certain configurations, such as to hold arbitrary pointers, not all combinations of the options are valid. With some combinations the map table may not work correctly, or may not even be initialized correctly.

### Subclassing Notes

`NSMapTable` is not suitable for subclassing.

## Topics

### Creating and Initializing a Map Table

[`init(keyOptions:valueOptions:capacity:)`](/documentation/Foundation/NSMapTable/init(keyOptions:valueOptions:capacity:))

Returns a map table, initialized with the given options.

[`init(keyOptions:valueOptions:)`](/documentation/Foundation/NSMapTable/init(keyOptions:valueOptions:))

Returns a new map table, initialized with the given options

[`init(keyPointerFunctions:valuePointerFunctions:capacity:)`](/documentation/Foundation/NSMapTable/init(keyPointerFunctions:valuePointerFunctions:capacity:))

Returns a map table, initialized with the given functions.

[`strongToStrongObjects()`](/documentation/Foundation/NSMapTable/strongToStrongObjects())

Returns a new map table object which has strong references to the keys and values.

[`weakToStrongObjects()`](/documentation/Foundation/NSMapTable/weakToStrongObjects())

Returns a new map table object which has weak references to the keys and strong references to the values.

[`strongToWeakObjects()`](/documentation/Foundation/NSMapTable/strongToWeakObjects())

Returns a new map table object which has strong references to the keys and weak references to the values.

[`weakToWeakObjects()`](/documentation/Foundation/NSMapTable/weakToWeakObjects())

Returns a new map table object which has weak references to the keys and values.

[`NSMapTableOptions`](/documentation/Foundation/NSMapTableOptions)

Constants used as components in a bitfield to specify the behavior of elements (keys and values) in an `NSMapTable` object.

### Accessing Content

[`object(forKey:)`](/documentation/Foundation/NSMapTable/object(forKey:))

Returns a the value associated with a given key.

[`keyEnumerator()`](/documentation/Foundation/NSMapTable/keyEnumerator())

Returns an enumerator object that lets you access each key in the map table.

[`objectEnumerator()`](/documentation/Foundation/NSMapTable/objectEnumerator())

Returns an enumerator object that lets you access each value in the map table.

[`count`](/documentation/Foundation/NSMapTable/count)

The number of key-value pairs in the map table.

### Manipulating Content

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

Adds a given key-value pair to the map table.

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

Removes a given key and its associated value from the map table.

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

Empties the map table of its entries.

### Creating a Dictionary Representation

[`dictionaryRepresentation()`](/documentation/Foundation/NSMapTable/dictionaryRepresentation())

Returns a dictionary representation of the map table.

### Accessing Pointer Functions

[`keyPointerFunctions`](/documentation/Foundation/NSMapTable/keyPointerFunctions)

The pointer functions the map table uses to manage keys.

[`valuePointerFunctions`](/documentation/Foundation/NSMapTable/valuePointerFunctions)

The pointer functions the map table uses to manage values.

[`NSPointerFunctions`](/documentation/Foundation/NSPointerFunctions)

An instance of `NSPointerFunctions` defines callout functions appropriate for managing a pointer reference held somewhere else.

### Deprecated

[`mapTableWithStrongToStrongObjects`](/documentation/Foundation/NSMapTable/mapTableWithStrongToStrongObjects)

Returns a new map table object which has strong references to the keys and values.

[`mapTableWithWeakToStrongObjects`](/documentation/Foundation/NSMapTable/mapTableWithWeakToStrongObjects)

Returns a new map table object which has weak references to the keys and strong references to the values.

[`mapTableWithStrongToWeakObjects`](/documentation/Foundation/NSMapTable/mapTableWithStrongToWeakObjects)

Returns a new map table object which has strong references to the keys and weak references to the values.

[`mapTableWithWeakToWeakObjects`](/documentation/Foundation/NSMapTable/mapTableWithWeakToWeakObjects)

Returns a new map table object which has weak references to the keys and values.

[Legacy Map Table Implementation](/documentation/Foundation/legacy-map-table-implementation)



---

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)