struct
AnyHashable
Inheritance |
CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable, Hashable
View Protocol Hierarchy →
|
---|---|
Import |
|
Initializers
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.
let
x
=
AnyHashable
(
Int
(
42
))
let
y
=
AnyHashable
(
UInt8
(
42
))
(
x
==
y
)
// Prints "false" because `Int` and `UInt8` are different types
(
x
==
AnyHashable
(
Int
(
42
)))
// Prints "true"
base
: A hashable value to wrap.
Declaration
init
<
H
:
Hashable
>
(
_
base
:
H
)
Instance Variables
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
{
(
unwrappedMessage
)
}
// Prints "Hello world!"
Declaration
var
base
:
Any
{
get
}
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
}
A textual representation of this instance, suitable for debugging.
Declaration
var
debugDescription
:
String
{
get
}
A textual representation of this instance.
Instead of accessing this property directly, convert an instance of any
type to a string by using the String(describing:)
initializer. For
example:
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
)
(
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
}
The hash value.
Hash values are not guaranteed to be equal across different executions of your program. Do not save hash values to use during a future execution.
Declaration
var
hashValue
:
Int
{
get
}
Instance Methods
Returns a Boolean value indicating whether two type-erased hashable instances wrap the same type and value.
Two instances of AnyHashable
compare as equal if and only if the
underlying types have the same conformance to the Equatable
protocol
and the underlying values compare as equal.
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.
let
x
=
AnyHashable
(
Int
(
42
))
let
y
=
AnyHashable
(
UInt8
(
42
))
(
x
==
y
)
// Prints "false" because `Int` and `UInt8` are different types
(
x
==
AnyHashable
(
Int
(
42
)))
// Prints "true"
Parameters: lhs: A type-erased hashable value. rhs: Another type-erased hashable value.
Declaration
func
==(
lhs
:
AnyHashable
,
rhs
:
AnyHashable
) -
>
Bool
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 inAnyHashable
instances: