0

When I reading the book Advanced Swift and in the Chapter 'Hashable Requirement', I got confused by this explanation

two instances that are equal (as defined by your == implementation) must have the same hash value. The reverse isn’t true: two instances with the same hash value don’t necessarily compare equally.

How can I comprehend the 'reverse' situation, or why do the two instances with the same hash value don’t necessarily compare equally.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
hsing
  • 114
  • 6

1 Answers1

1

Think of the hash value as a quick, compact, non-unique identifier for a given object instance. The only hard condition is this: if two objects compare equally, according to the == operator, than both instances must have the exact same hash value. That’s all there is to it ;)

In particular, given that hash values aren’t unique — and how they could be given Int limited range? — we can’t safely assume that two instances with the same hash value will compare equally.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85