protocol
Hashable
A type that can be hashed into a Hasher
to produce an integer hash value.
Inheritance | Equatable |
---|---|
Conforming Types | AnyHashable, AnyKeyPath, BinaryInteger, Bool, Character, CodingUserInfoKey, CollectionDifference.Index, Dictionary.Index, Double, Float, Float80, FloatingPoint, Int, Int16, Int32, Int64, Int8, ObjectIdentifier, OpaquePointer, SIMD, Set, Set.Index, String, String.Index, StringProtocol, UInt, UInt16, UInt32, UInt64, UInt8, Unicode.Scalar |
Instance Variables
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.
Important:
hashValue
is deprecated as aHashable
requirement. To conform toHashable
, implement thehash(into:)
requirement instead.
Declaration
var
hashValue
:
Int
Instance Methods
Hashes the essential components of this value by feeding them into the given hasher.
Implement this method to conform to the Hashable
protocol. The
components used for hashing must be the same as the components compared
in your type's ==
operator implementation. Call hasher.combine(_:)
with each of these components.
Important: Never call
finalize()
onhasher
. Doing so may become a compile-time error in the future.
- Parameter hasher: The hasher to use when combining the components of this instance.
Declaration
func
hash
(
into
hasher
:
inout
Hasher
)
Default Implementations
Declaration
public
static
func
!=(
lhs
:
Self
,
rhs
:
Self
) -
>
Bool
You can use any type that conforms to the
Hashable
protocol in a set or as a dictionary key. Many types in the standard library conform toHashable
: Strings, integers, floating-point and Boolean values, and even sets are hashable by default. Some other types, such as optionals, arrays and ranges automatically become hashable when their type arguments implement the same.Your own custom types can be hashable as well. When you define an enumeration without associated values, it gains
Hashable
conformance automatically, and you can addHashable
conformance to your other custom types by implementing thehash(into:)
method. For structs whose stored properties are allHashable
, and for enum types that have all-Hashable
associated values, the compiler is able to provide an implementation ofhash(into:)
automatically.Hashing a value means feeding its essential components into a hash function, represented by the
Hasher
type. Essential components are those that contribute to the type's implementation ofEquatable
. Two instances that are equal must feed the same values toHasher
inhash(into:)
, in the same order.Conforming to the Hashable Protocol
To use your own custom type in a set or as the key type of a dictionary, add
Hashable
conformance to your type. TheHashable
protocol inherits from theEquatable
protocol, so you must also satisfy that protocol's requirements.The compiler automatically synthesizes your custom type's
Hashable
and requirements when you declareHashable
conformance in the type's original declaration and your type meets these criteria:To customize your type's
Hashable
conformance, to adoptHashable
in a type that doesn't meet the criteria listed above, or to extend an existing type to conform toHashable
, implement thehash(into:)
method in your custom type.In your
hash(into:)
implementation, callcombine(_:)
on the providedHasher
instance with the essential components of your type. To ensure that your type meets the semantic requirements of theHashable
andEquatable
protocols, it's a good idea to also customize your type'sEquatable
conformance to match.As an example, consider a
GridPoint
type that describes a location in a grid of buttons. Here's the initial declaration of theGridPoint
type:You'd like to create a set of the grid points where a user has already tapped. Because the
GridPoint
type is not hashable yet, it can't be used in a set. To addHashable
conformance, provide an==
operator function and implement thehash(into:)
method.The
hash(into:)
method in this example feeds the grid point'sx
andy
properties into the provided hasher. These properties are the same ones used to test for equality in the==
operator function.Now that
GridPoint
conforms to theHashable
protocol, you can create a set of previously tapped grid points.