Generic Class

NSHash​Table

NSHash​Table is modeled after NSSet but provides different options, in particular to support weak relationships.

Overview

  • 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​Table instance to operate on arbitrary pointers and not just objects, although typically you are encouraged to use the C function API for void * pointers. (See Hash Tables for more information) The object-based API (such as add(_:​)) will not work for non-object pointers without type-casting.

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

When configuring hash tables, note that only the options listed in NSHash​Table​Options guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration. While other NSPointer​Functions 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​Table is not suitable for subclassing.

Symbols

Initialization

init(options:​ NSPointer​Functions.Options = [], capacity:​ Int)

Returns a hash table initialized with the given attributes.

init(pointer​Functions:​ NSPointer​Functions, capacity:​ Int)

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

Convenience Constructors

class func weak​Objects()

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

init(options:​ NSPointer​Functions.Options = [])

Returns a hash table with given pointer functions options.

Accessing Content

var any​Object:​ Object​Type?

One of the objects in the hash table.

var all​Objects:​ [Object​Type]

The hash table’s members.

var set​Representation:​ Set<Any​Hashable>

A set that contains the hash table’s members.

var count:​ Int

The number of elements in the hash table.

func contains(Object​Type?)

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

func member(Object​Type?)

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

func object​Enumerator()

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

Manipulating Membership

func add(Object​Type?)

Adds a given object to the hash table.

func remove(Object​Type?)

Removes a given object from the hash table.

func remove​All​Objects()

Removes all objects from the hash table.

Comparing Hash Tables

func intersect(NSHash​Table<Object​Type>)

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

func intersects(NSHash​Table<Object​Type>)

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

func is​Subset(of:​ NSHash​Table<Object​Type>)

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

func is​Equal(to:​ NSHash​Table<Object​Type>)

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

Set Functions

func minus(NSHash​Table<Object​Type>)

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

func union(NSHash​Table<Object​Type>)

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

Accessing Pointer Functions

var pointer​Functions:​ NSPointer​Functions

The pointer functions for the hash table.

Constants

NSHash​Table​Options

Components in a bit-field to specify the behavior of elements in an NSHash​Table object.

Relationships

Generic Constraints

  • ObjectType : AnyObject

Inherits From