Structure

AnyHashable

A type-erased hashable value.

Overview

The AnyHashable type forwards equality comparisons and hashing operations to an underlying hashable value, hiding its specific underlying type.

You can store mixed-type keys in dictionaries and other collections that require Hashable conformance by wrapping mixed-type keys in AnyHashable instances:

let descriptions: [AnyHashable: Any] = [
    AnyHashable("😄"): "emoji",
    AnyHashable(42): "an Int",
    AnyHashable(Int8(43)): "an Int8",
    AnyHashable(Set(["a", "b"])): "a set of strings"
]
print(descriptions[AnyHashable(42)]!)      // prints "an Int"
print(descriptions[AnyHashable(43)])       // prints "nil"
print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
print(descriptions[AnyHashable(Set(["a", "b"]))]!) // prints "a set of strings"

Topics

Initializers

init<H>(H)

Creates a type-erased hashable value that wraps the given instance.

Operator Functions

static func !=(AnyHashable, AnyHashable)

Returns a Boolean value indicating whether two values are not equal.

Beta
static func ==(AnyHashable, AnyHashable)

Returns a Boolean value indicating whether two type-erased hashable instances wrap the same type and value.

See Also

Type-Erasing Wrappers

struct AnyCollection

A type-erased wrapper over any collection with indices that support forward traversal.

struct AnyBidirectionalCollection

A type-erased wrapper over any collection with indices that support bidirectional traversal.

struct AnyRandomAccessCollection

A type-erased wrapper over any collection with indices that support random access traversal.

struct AnyIterator

A type-erased iterator of Element.

struct AnyIndex

A wrapper over an underlying index that hides the specific underlying type.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software