Say I had a class called Example, it conforms to Hashable via its ID attribute.
I have an Example object exam1, with ID = 6 and name = "goon". I set a dictionary entry dict[exam1] = 7.
Then lets say I had another example object with the same ID, but different data inside. Let's call it exam2, with ID = 6, name = "pie".
If I set dict[exam2] = 8, this would index to the same entry as before because their IDs are the same. However, would the key be updated to reference exam2? so dict.keys would contain reference to exam2, not exam1.
Dictionary will contain instance of
exam1, notexam2. Reason: as the documentation statesIn other words, the assignment
dict[x] = ycan modify the value, but will never modify the key, it can only add it (if the key is not yet in dictionary) or remove it (if the value is nil).But as someone mentioned above, this is a really bad idea. The meaning of
HashableisAnd that takes us to the meaning of Equatable (which you will also have to implement as a part of conforming to Hashable:
So when you defined your Hashable, as "ID only", you state that
nameis not an essential component and thatexam1 == exam2(even though theirnameproperty has different values). This is legal from the language perspective, but a huge potential to bugs and issues in the future.