From Hashable
documentation:
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.
your code:
static func == (lhs: Vorlesung, rhs: Vorlesung) -> Bool {
let result = lhs.subject.compare(rhs.subject) == .orderedSame
return result
}
func hash(into hasher: inout Hasher) {
hasher.combine(vID) // UUID
}
Currently your hash
and ==
are using completely different properties, which leads to conflict between them. Are two Vorlesung with the same subject but different vID equal to each other, or not? According to ==
they are equal, but according to hash
they are not. We need to fix that
You need to decide what makes two Vorlesung
the same, and use these properties in both ==
and hash
. Usually in a struct it's all of the properties, and that's handled by the autogenerated implementation