Skip to content

Instantly share code, notes, and snippets.

@ctreffs
Last active January 14, 2021 07:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctreffs/88c9d3be2309e5caff08637527aac367 to your computer and use it in GitHub Desktop.
Save ctreffs/88c9d3be2309e5caff08637527aac367 to your computer and use it in GitHub Desktop.
Memory address String of AnyObject instance in Swift
extension MemoryLayout where T: AnyObject {
/// Returns the memory address of the given instance.
/// - Parameter instance: The instance to inspect.
/// - Returns: A hex string representation of the memory address without leading zeros (e.g. `0x102f6dda0`).
@inline(__always)
public static func address(of instance: T) -> String {
"0x"+String(UInt(bitPattern: ObjectIdentifier(instance)), radix: 16, uppercase: false)
}
}