A collection similar to a set, but with broader range of available memory semantics.
SDKs
- iOS 6.0+
- macOS 10.5+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
class NSHashTable<ObjectType> : NSObject where ObjectType : AnyObject
Overview
The hash table is modeled after 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 NSHash
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(_:)
) will not work for non-object pointers without type-casting.
Because of its options, NSHash
is not a set because it can behave differently (for example, if pointer equality is specified two is
strings will both be entered).
When configuring hash tables, note that only the options listed in NSHash
guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration. While other NSPointer
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
NSHash
is not suitable for subclassing.