Last active
January 14, 2021 07:20
-
-
Save ctreffs/88c9d3be2309e5caff08637527aac367 to your computer and use it in GitHub Desktop.
Memory address String of AnyObject instance in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
For reference see how Swift does it internally: