import UIKit
let p = Person("123", id: 123)
Person.value = "123"
let b = Person("123", id: 123 )
let s = "123"
print(p.hashValue)
print(b.hashValue)
print(p == b)
class Person: Hashable {
static var value: String?
var name: String?
var id: Int?
init(_ name: String, id: Int){
self.name = name
self.id = id
}
func hash(into hasher: inout Hasher){
//hasher.combine去帮我们实现了hashValue
//内存地址来判断是否相等
let add = address(o: self)
hasher.combine(add)
//通过自身的属性来判断是否相等
// hasher.combine(name)
// hasher.combine(id)
}
}
func == (lhs: Person, rhs: Person) -> Bool {
return lhs.hashValue == rhs.hashValue
}
func address<T: AnyObject>(o: T) -> String {
return String.init(format: "%018p", unsafeBitCast(o, to: Int.self))
}
网友评论