Junior — Senior
What output will the program produce
livecode
Task condition
Consider the given Swift code. The KeyObject structure implements the Hashable protocol, but in the hash(into:) method, it always passes the same value to the hasher, so all its instances will have the same hash. Then, two key objects are created, a value is assigned to the first in the dictionary, and then this same element is printed.
struct KeyObject: Hashable {
let a: Int
func hash(into hasher: inout Hasher) {
hasher.combine(0)
}
}
let firstKey = KeyObject(a: 3)
let secondKey = KeyObject(a: 1)
var map: [KeyObject: String] = [:]
map[firstKey] = "hi"
print(map[firstKey])
What will be printed?