Rumah > Soal Jawab > teks badan
func test(_ arr: [String]) {
for var i in arr {
withUnsafePointer(to: &i) {
print(" str value \(i) has address: \($0)")
}
}
}
var a = ["1","2","3"]
test(a)
打印结果:
str value 1 has address: 0x00007fff5dff9fc0
str value 2 has address: 0x00007fff5dff9fc0
str value 3 has address: 0x00007fff5dff9fc0
如果我想打印整个数组的地址怎么打?
如果数组里不是字符串,是其他类型,改怎么打?找了很久没找到,特此来提问
伊谢尔伦2017-04-18 09:53:11
// 数组,无论什么数组都可以
let str = ["hello", "world", "!\r\n"]
// 查看一下数组
print(str)
//打印数组的地址,而不是单个元素的地址
str.withUnsafeBytes { print(public class Ball {
var color = 0
var tag = "foot"
public init(color: Int = 0, tag: String = "beach") {
self.color = color
self.tag = tag
}
}
var ball = Ball()
/// 获取对象指针并打印地址
withUnsafePointer(to: &ball) { pointer in
print(pointer)
}
) }
rrreee