I want to send a store a method address in a class, allowing the class to call that method. I want this class to be Hashable, and I want the hashValue to be calculated from the method such that 2 objects pointing to the same method have the same hashValue.
Here is the class init that attempts to calculate the hashValue, but it fails. I think it hashes a pointer to the local variable.
typealias functionAlias = ((Double) -> ())?
class FunctionPointer: Hashable {
private let functionPointer: functionAlias
private let hash: Int
init( functionPointer: functionAlias ) {
self.functionPointer = functionPointer
var f = functionPointer
var h = 0
withUnsafePointer(to: &f, {
ptr in
h = ptr.hashValue
})
self.hash = h
print("self.hash = \(self.hash)")
}
}
Thanks