快速打印可变内存地址 [英] Printing a variable memory address in swift

查看:83
本文介绍了快速打印可变内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正有没有用新的Swift语言从Objective-C模拟[NSString stringWithFormat:@"%p", myVar]?

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

例如:

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

推荐答案

Swift 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)")
}

这篇关于快速打印可变内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆