Dla Swift 3 (Xcode 8 beta 6 lub nowszy), użyj ObjectIdentifier
.
class Test : Hashable {
// Swift 2:
var hashValue: Int { return unsafeAddressOf(self).hashValue }
// Swift 3:
var hashValue: Int { return ObjectIdentifier(self).hashValue }
}
func ==(lhs: Test, rhs: Test) -> Bool {
return lhs === rhs
}
Następnie a == b
IFF a
i b
odnoszą się do tej samej instancji klasy ( i a.hashValue == b.hashValue
jest spełniony w tym przypadku).
przykład:
var dictionary = [Test: String]()
let a = Test()
let b = Test()
dictionary[a] = "A"
print(dictionary[a]) // Optional("A")
print(dictionary[b]) // nil
Swift 2,3 i powyżej, można użyć
/// Return an UnsafePointer to the storage used for `object`. There's
/// not much you can do with this other than use it to identify the
/// object
func unsafeAddressOf(object: AnyObject) -> UnsafePointer<Void>
zaimplementować wartość skrótu, a operator tożsamość ===
do wdrożenia protokołu Equatable
.
'unsafeAddressOf' jest to, czego szukaliśmy. Dzięki! – Kirsteins
Czy będzie przypadek, w którym 'unsafeAddressOf' zwróci inny adres dla tej samej zmiennej? Wydaje mi się, że wpadłem na ten problem i nie mogę go odtworzyć. Ten adres na początku mówi jedno, a potem zmieni się, jakby Swift przesunął zmienną w pamięci gdzie indziej. – pixelfreak
Nie dotyczy instancji klasy * *. Ale zobacz http://stackoverflow.com/questions/32638879/swift-strings-and-memory-addresses dla przypadku, w którym miałoby to miejsce dla pewnych * typów wartości *, które są niejawnie połączone z pewnym NSObject. –