Skip to content

Instantly share code, notes, and snippets.

@ctreffs
Last active January 14, 2021 07:20
Show Gist options
  • 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment