Dictionary will contain instance of exam1
, not exam2
. Reason: as the documentation states
Update an existing value by assigning a new value to a key that already exists in the dictionary.
In other words, the assignment dict[x] = y
can 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 Hashable
is
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 of Equatable. Two instances that are equal must feed the same values to Hasher in hash(into:), in the same order.
And that takes us to the meaning of Equatable (which you will also have to implement as a part of conforming to Hashable:
Equality implies substitutability—any two instances that compare equally can be used interchangeably in any code that depends on their values.
So when you defined your Hashable, as "ID only", you state that name
is not an essential component and that exam1 == exam2
(even though their name
property has different values). This is legal from the language perspective, but a huge potential to bugs and issues in the future.