Hashable
Intro
Source code
Kodeco
class Person: Equatable, Hashable {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
static func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age
}
func hash(into hasher: inout Hasher) {
hasher.combine(name)
hasher.combine(age)
}
}
let john = Person(name: "John", age: 30)
let jane = Person(name: "Jane", age: 25)
let personDict = [john: "uno", jane: "dos"]
print(personDict[jane]!) // Output: "dos"
Quirks
Objective-C Interop with Swift
NSObject subclass in Swift: hash vs hashValue, isEqual vs ==