<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/AnyHashable",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11AnyHashableV"
  },
  "title" : "AnyHashable"
}
-->

# AnyHashable

A type-erased hashable value.

```
@frozen struct AnyHashable
```

## Overview

The `AnyHashable` type forwards equality comparisons and hashing operations
to an underlying hashable value, hiding the type of the wrapped value.

Where conversion using `as` or `as?` is possible between two types (such as
`Int` and `NSNumber`), `AnyHashable` uses a canonical representation of the
type-erased value so that instances wrapping the same value of either type
compare as equal. For example, `AnyHashable(42)` compares as equal to
`AnyHashable(42 as NSNumber)`.

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] = [
    42: "an Int",
    43 as Int8: "an Int8",
    ["a", "b"] as Set: "a set of strings"
]
print(descriptions[42]!)                // prints "an Int"
print(descriptions[42 as Int8]!)        // prints "an Int"
print(descriptions[43 as Int8]!)        // prints "an Int8"
print(descriptions[44])                 // prints "nil"
print(descriptions[["a", "b"] as Set]!) // prints "a set of strings"
```

Note that `AnyHashable` does not guarantee that it preserves the hash
encoding of wrapped values. Do not rely on `AnyHashable` generating such
compatible hashes, as the hash encoding that it uses may change between any
two releases of the standard library.

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`Copyable`](/documentation/Swift/Copyable)

[`Escapable`](/documentation/Swift/Escapable)

---

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)