一尘不染

快速打印可变内存地址

swift

无论如何,是否可以[NSString stringWithFormat:@"%p", myVar]使用新的Swift语言从Objective-C 模拟?

例如:

let str = "A String"
println(" str value \(str) has address: ?")

阅读 255

收藏
2020-07-07

共1个答案

一尘不染

迅捷2

现在,它已成为标准库的一部分:unsafeAddressOf

/// Return an UnsafePointer to the storage used for `object`.  There's
/// not much you can do with this other than use it to identify the
/// object

迅捷3

对于Swift 3,请使用withUnsafePointer

var str = "A String"
withUnsafePointer(to: &str) {
    print(" str value \(str) has address: \($0)")
}
2020-07-07