<!--
{
  "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/NSHashTable",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSHashTable"
  },
  "title" : "NSHashTable"
}
-->

# NSHashTable

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

```
class NSHashTable<ObjectType> where ObjectType : AnyObject
```

## Overview

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

- It can hold weak references to its members.
- Its members may be copied on input or may use pointer identity for equality and hashing.
- It can contain arbitrary pointers (its members are not constrained to being objects).

You can configure an [`NSHashTable`](/documentation/Foundation/NSHashTable) 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 [`add(_:)`](/documentation/Foundation/NSHashTable/add(_:))) will not work for non-object pointers without type-casting.

Because of its options, `NSHashTable` is not a set because it can behave differently (for example, if pointer equality is specified two `isEqual:` strings will both be entered).

When configuring hash tables, note that only the options listed in [`NSHashTableOptions`](/documentation/Foundation/NSHashTableOptions) 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 hash table may not work correctly, or may not even be initialized correctly.

### Subclassing Notes

`NSHashTable` is not suitable for subclassing.

## Topics

### Initialization

[`init(options:capacity:)`](/documentation/Foundation/NSHashTable/init(options:capacity:))

Returns a hash table initialized with the given attributes.

[`init(pointerFunctions:capacity:)`](/documentation/Foundation/NSHashTable/init(pointerFunctions:capacity:))

Returns a hash table initialized with the given functions and capacity.

### Convenience Constructors

[`weakObjects()`](/documentation/Foundation/NSHashTable/weakObjects())

Returns a new hash table for storing weak references to its contents.

[`init(options:)`](/documentation/Foundation/NSHashTable/init(options:))

Returns a hash table with given pointer functions options.

### Accessing Content

[`anyObject`](/documentation/Foundation/NSHashTable/anyObject)

One of the objects in the hash table.

[`allObjects`](/documentation/Foundation/NSHashTable/allObjects)

The hash table’s members.

[`setRepresentation`](/documentation/Foundation/NSHashTable/setRepresentation)

A set that contains the hash table’s members.

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

The number of elements in the hash table.

[`contains(_:)`](/documentation/Foundation/NSHashTable/contains(_:))

Returns a Boolean value that indicates whether the hash table contains a given object.

[`member(_:)`](/documentation/Foundation/NSHashTable/member(_:))

Determines whether the hash table contains a given object, and returns that object if it is present

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

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

### Manipulating Membership

[`add(_:)`](/documentation/Foundation/NSHashTable/add(_:))

Adds a given object to the hash table.

[`remove(_:)`](/documentation/Foundation/NSHashTable/remove(_:))

Removes a given object from the hash table.

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

Removes all objects from the hash table.

### Comparing Hash Tables

[`intersect(_:)`](/documentation/Foundation/NSHashTable/intersect(_:))

Removes from the receiving hash table each element that isn’t a member of another given hash table.

[`intersects(_:)`](/documentation/Foundation/NSHashTable/intersects(_:))

Returns a Boolean value that indicates whether a given hash table intersects with the receiving hash table.

[`isSubset(of:)`](/documentation/Foundation/NSHashTable/isSubset(of:))

Returns a Boolean value that indicates whether every element in the receiving hash table is also present in another given hash table.

[`isEqual(to:)`](/documentation/Foundation/NSHashTable/isEqual(to:))

Returns a Boolean value that indicates whether a given hash table is equal to the receiving hash table.

### Set Functions

[`minus(_:)`](/documentation/Foundation/NSHashTable/minus(_:))

Removes each element in another given hash table from the receiving hash table, if present.

[`union(_:)`](/documentation/Foundation/NSHashTable/union(_:))

Adds each element in another given hash table to the receiving hash table, if not present.

### Accessing Pointer Functions

[`pointerFunctions`](/documentation/Foundation/NSHashTable/pointerFunctions)

The pointer functions for the hash table.

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

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

### Constants

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

Components in a bit-field to specify the behavior of elements in an [`NSHashTable`](/documentation/Foundation/NSHashTable) object.

### Deprecated

[`hashTableWithWeakObjects`](/documentation/Foundation/NSHashTable/hashTableWithWeakObjects)

Returns a new hash table for storing weak references to its contents.

[Legacy Hash Table Implementation](/documentation/Foundation/legacy-hash-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)