Is it possible to hash a struct without randomization natively in Swift?
struct Kiwi: Hashable {
let userId: String
let selections: Set<Int>
init(userId: String, selections: Set<Int>) {
self.userId = userId
self.selections = selections
}
func hash(into hasher: inout Hasher) {
hasher.combine(userId)
hasher.combine(selections)
}
}
let k1 = Kiwi(userId: "u1", selections: [1])
print(k1.hashValue)
let k2 = Kiwi(userId: "u1", selections: [1])
print(k2.hashValue)
These hash values are not guaranteed to be equal across different points of execution (due to randomization of the hashing function). The purpose of this hash is simply to generate a signature of a struct's values so that they are always the same if the values of the struct are the same, regardless of when it's executed.
For the time being, I've made the struct Equatable
and just compare struct instances but would prefer to be able to compare "signatures" (i.e. hashes) instead.