aleclarson aleclarson - 4 months ago 24
Swift Question

Get a unique String for a given AnyObject?

In Objective-C, this was as simple as:

[NSString stringWithFormat:@"%p", objRef]


How can I do this in Swift?

Answer
func hashString (obj: AnyObject) -> String {
  return String(ObjectIdentifier(obj).uintValue)
}

let id = hashString(obj)

Swift 3.0

Either:

return String(UInt(ObjectIdentifier(obj))

Or:

return String(ObjectIdentifer(obj).hashValue)