Casting a pointer to UInt in Swift

October 15, 2018

There are some times in Swift, like when using System Trace, that you want to get the pointer value of an object directly as a UInt (passing the pointer to kdebug_signpost is one). I was surprised at how hard it was to find documentation on how to get a Swift reference’s pointer value as a UInt. Here’s how to do it, using the UInt(bitPattern:) initializer:

_ = withUnsafePointer(to: myObject) { unsafePointer in
    let pointerValue = UInt(bitPattern: unsafePointer)
    // do whatever with pointerValue: it's a UInt!