Printing a variable memory address in swift

前端 未结 15 1024
天涯浪人
天涯浪人 2020-11-28 01:16

Is there anyway to simulate the [NSString stringWithFormat:@\"%p\", myVar], from Objective-C, in the new Swift language?

For example:

le         


        
15条回答
  •  [愿得一人]
    2020-11-28 01:32

    The other answers are fine, though I was looking for a way to get the pointer address as an integer:

    let ptr = unsafeAddressOf(obj)
    let nullPtr = UnsafePointer(bitPattern: 0)
    
    /// This gets the address of pointer
    let address = nullPtr.distanceTo(ptr) // This is Int
    

    Just a little follow-up.

提交回复
热议问题