Hashable

정수 해시 값을 생성할 수 있는 유형

Apple Developer Documentation

<aside> 💬 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 to Hashable: 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 add Hashableconformance to your other custom types by implementing the hash(into:) method. For structs whose stored properties are all Hashable, and for enum types that have all-Hashableassociated values, the compiler is able to provide an implementation of hash(into:)automatically.

</aside>

→ 기본적으로 Hashable 프로토콜을 따는 타입이 여러가지 있다.

: String, Int, Float, Bool, Set, Enum without associated value, Struct(stored properties)

→ Hashable 프로토콜을 따르게 해주려면, hash(into:) 메소드를 사용해서 조건을 충족시켜주면 된다.

: 아래가 그 조건임

스크린샷 2021-12-07 오후 10.45.36.png

Hash?

스크린샷 2021-12-07 오후 10.29.11.png

스크린샷 2021-12-07 오후 10.29.41.png

설명 출처 - https://zeddios.tistory.com/498

예시 코드

Hashable 프로토콜을 채택해줄 때 몇 가지 일을 더 해줘야 한다.