是否有任何方式来模拟[NSString stringWithFormat:@“%p”,myVar],从Objective-C,在新的Swift语言?

例如:

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

当前回答

这是为了Swift 3。

像@CharlieMonroe一样,我想以整数形式获取地址。具体来说,我想要Thread对象的地址作为诊断日志记录模块中的线程ID,用于没有线程名可用的情况。

根据查理·门罗的代码,这是我目前想出的。但注意,我对霉霉很陌生,这可能不正确……

  // Convert the memory address of the current Thread object into an Int for use as a thread ID
  let objPtr = Unmanaged.passUnretained(Thread.current).toOpaque()
  let onePtr = UnsafeMutableRawPointer(bitPattern: 1)!  // 1 used instead of 0 to avoid crash
  let rawAddress : Int64 = onePtr.distance(to: objPtr) + 1  // This may include some high-order bits
  let address = rawAddress % (256 * 1024 * 1024 * 1024)  // Remove high-order bits

最后一条语句存在,因为如果没有它,我将获得像0x60000007DB3F这样的地址。最后一条语句中的模运算将其转换为0x7DB3F。

其他回答

这是为了Swift 3。

像@CharlieMonroe一样,我想以整数形式获取地址。具体来说,我想要Thread对象的地址作为诊断日志记录模块中的线程ID,用于没有线程名可用的情况。

根据查理·门罗的代码,这是我目前想出的。但注意,我对霉霉很陌生,这可能不正确……

  // Convert the memory address of the current Thread object into an Int for use as a thread ID
  let objPtr = Unmanaged.passUnretained(Thread.current).toOpaque()
  let onePtr = UnsafeMutableRawPointer(bitPattern: 1)!  // 1 used instead of 0 to avoid crash
  let rawAddress : Int64 = onePtr.distance(to: objPtr) + 1  // This may include some high-order bits
  let address = rawAddress % (256 * 1024 * 1024 * 1024)  // Remove high-order bits

最后一条语句存在,因为如果没有它,我将获得像0x60000007DB3F这样的地址。最后一条语句中的模运算将其转换为0x7DB3F。

斯威夫特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)")
}

注意:这是针对引用类型的。

斯威夫特:4/5

print(Unmanaged.passUnretained(someVar).toOpaque())

打印someVar的内存地址。 (感谢@Ying)


斯威夫特3.1:

print(Unmanaged<AnyObject>.passUnretained(someVar as AnyObject).toOpaque())

打印someVar的内存地址。


在Swift4 about Array中:

    let array1 = [1,2,3]
    let array2 = array1
    array1.withUnsafeBufferPointer { (point) in
        print(point) // UnsafeBufferPointer(start: 0x00006000004681e0, count: 3)
    }
    array2.withUnsafeBufferPointer { (point) in
        print(point) // UnsafeBufferPointer(start: 0x00006000004681e0, count: 3)
    }

引用类型:

获取引用类型的内存地址是有意义的,因为它表示标识。 ===标识运算符用于检查两个对象是否指向同一个引用。 使用ObjectIdentifier获取内存地址

代码:

class C {}

let c1 = C()
let c2 = c1

//Option 1:
print("c1 address: \(Unmanaged.passUnretained(c1).toOpaque())") 

//Option 2:
let o1 = ObjectIdentifier(c1)
let o2 = ObjectIdentifier(c2)

print("o1 -> c1 = \(o1)")
print("o2 -> c2 = \(o2)")

if o1 == o2 {
    print("c1 = c2")
} else {
    print("c1 != c2")
}

//Output:
//c1 address: 0x000060c000005b10
//o1 -> c1 = ObjectIdentifier(0x000060c000005b10)
//o2 -> c2 = ObjectIdentifier(0x000060c000005b10)
//c1 = c2

值类型:

获取值类型的内存地址的需求并不太重要(因为它是一个值),重点将更多地放在值的相等性上。