Using XCode I discovered that importing Foundation adds a property hash: Int?
to AnyObject:
import Foundation
let str = "Hello, playground"
let int = 12
let double = 1.0
let bool = false
class A: Hashable {
var i = 2
static func == (lhs: A, rhs: A) -> Bool {
return lhs.i == rhs.i
}
func hash(into hasher: inout Hasher) {
hasher.combine(i)
}
}
class B {
var b = false
}
class C {
var b: B? = nil
}
let a = A()
let b = B()
let c = C()
(str as AnyObject).hash // some number
(int as AnyObject).hash // some number
(double as AnyObject).hash // some number
(bool as AnyObject).hash // 0
(a as AnyObject).hash // some number
(b as AnyObject).hash // some number
(c as AnyObject).hash // some number
(c as AnyObject).hash == (c as AnyObject).hash // true
let a2 = A()
(a as AnyObject).hash == (a2 as AnyObject).hash // false
However, I googled till my fingers bleeded, but I could not find any documentation about this except the sentence XCode displays: "Returns an Integer that can be used as a table address in a hash table structure". Also the Jump to Definition results arent very helpfull:
Many things remain unclear to me: Why is it optional? When will it return nil? Is it identity based, as appears to be on first sight... Is it safe to use it, if I want to create a Dictionary of AnyObjects => Why is AnyObject not Hashable then?
Pointers to the corresponding documentation or any answers to the questions above would be very much apprechiated