Junior — Middle
Can you name protocols that provide object hashing in Swift?
sobes.tech AI
Answer from AI
In Swift, the Hashable protocol is used to ensure object hashing. It requires the implementation of the hash(into:) method, which computes the hash value of the object. There is also the Equatable protocol, which is often used together with Hashable to determine object equality.
Example:
struct Person: Hashable {
var name: String
var age: Int
func hash(into hasher: inout Hasher) {
hasher.combine(name)
hasher.combine(age)
}
}
Thus, Hashable is the main protocol for object hashing in Swift.