AnyHashable

A type-erased hashable value.

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:

Inheritance CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable, Hashable View Protocol Hierarchy →
Import
  • import Swift

Initializers

init(_:)

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

The following example creates two type-erased hashable values: x wraps an Int with the value 42, while y wraps a UInt8 with the same numeric value. Because the underlying types of x and y are different, the two variables do not compare as equal despite having equal underlying values.

base: A hashable value to wrap.

Declaration

  • init<H>(_ base: H)

Instance Variables

var base: Any

The value wrapped by this instance.

The base property can be cast back to its original type using one of the casting operators (as?, as!, or as).

  • let anyMessage = AnyHashable("Hello world!")
  • if let unwrappedMessage = anyMessage.base as? String {
  •     print(unwrappedMessage)
  • }
  • // Prints "Hello world!"

Declaration

  • var base: Any { get }
var customMirror: Mirror

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

  • var customMirror: Mirror { get }
var debugDescription: String

A textual representation of this instance, suitable for debugging.

Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the String(reflecting:) initializer. This initializer works with any type, and uses the custom debugDescription property for types that conform to CustomDebugStringConvertible:

  • struct Point: CustomDebugStringConvertible {
  •     let x: Int, y: Int
  •  
  •     var debugDescription: String {
  •         return "(\(x), \(y))"
  •     }
  • }
  •  
  • let p = Point(x: 21, y: 30)
  • let s = String(reflecting: p)
  • print(s)
  • // Prints "(21, 30)"

The conversion of p to a string in the assignment to s uses the Point type's debugDescription property.

Declaration

  • var debugDescription: String { get }
var description: String

A textual representation of this instance.

Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the String(describing:) initializer. This initializer works with any type, and uses the custom description property for types that conform to CustomStringConvertible:

  • struct Point: CustomStringConvertible {
  •     let x: Int, y: Int
  •  
  •     var description: String {
  •         return "(\(x), \(y))"
  •     }
  • }
  •  
  • let p = Point(x: 21, y: 30)
  • let s = String(describing: p)
  • print(s)
  • // Prints "(21, 30)"

The conversion of p to a string in the assignment to s uses the Point type's description property.

Declaration

  • var description: String { get }
var hashValue: Int

The hash value.

Declaration

  • var hashValue: Int { get }

Instance Methods

func hash(into:)

Hashes the essential components of this value by feeding them into the given hasher.

hasher: The hasher to use when combining the components of this instance.

Declaration

  • func hash(into hasher: inout Hasher)