Swift3 通过单例重复创建对象,但对象地址不同

Swift3 通过单例重复创建对象,但对象地址不同,swift3,singleton,Swift3,Singleton,这是我的代码: class Test { static let sharedInstance = Test() private init() {} } var obj1 = Test.sharedInstance var obj2 = Test.sharedInstance withUnsafePointer(to: &obj1) { print(" obj1 value \(obj1) has address: \($0)") } withUnsafePo

这是我的代码:

class Test {
    static let sharedInstance = Test()
    private init() {}
}

var obj1 = Test.sharedInstance
var obj2 = Test.sharedInstance

withUnsafePointer(to: &obj1) {
    print(" obj1 value \(obj1) has address: \($0)")
}

withUnsafePointer(to: &obj2) {
    print(" obj2 value \(obj2) has address: \($0)")
}
日志:

obj1值测试的地址为:0x0000000112cc5bf8

obj2值测试的地址为:0x0000000112C5C00


提前感谢!

您的代码显示了两个变量的地址,
obj1
obj2

import Foundation

class Test {
    static let sharedInstance = Test()
    init() {} //Make initializer accessible for testing.
}

var obj1 = Test.sharedInstance
var obj2 = Test.sharedInstance

withUnsafePointer(to: &obj1) {
    print(" var obj1 is located at address: \($0)")
}
withUnsafePointer(to: &obj2) {
    print(" var obj2 is located at address: \($0)")
}
obj2 = Test() //Create new instance.
withUnsafePointer(to: &obj2) {
    print(" var obj2 is still located at address: \($0)")
}
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0
如果要比较
obj1
obj2
中包含的引用,使用
ObjectIdentifier
将是一种简单的方法:

obj2 = Test.sharedInstance
print(" ObjectIdentifier of obj1 is \(ObjectIdentifier(obj1))")
print(" ObjectIdentifier of obj2 is \(ObjectIdentifier(obj2))")
obj2 = Test()
print(" ObjectIdentifier of obj2 is now \(ObjectIdentifier(obj2))")
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0
或者,您可以将引用转换为不透明指针,然后再转换为int,如下所示:

let addrInt1 = Int(bitPattern: Unmanaged.passRetained(obj1).toOpaque())
print("Address of obj1 = \(String(format: "%016lX", addrInt1))")
let addrInt2 = Int(bitPattern: Unmanaged.passRetained(obj2).toOpaque())
print("Address of obj2 = \(String(format: "%016lX", addrInt2))")
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0

您的代码显示两个变量的地址,
obj1
obj2

import Foundation

class Test {
    static let sharedInstance = Test()
    init() {} //Make initializer accessible for testing.
}

var obj1 = Test.sharedInstance
var obj2 = Test.sharedInstance

withUnsafePointer(to: &obj1) {
    print(" var obj1 is located at address: \($0)")
}
withUnsafePointer(to: &obj2) {
    print(" var obj2 is located at address: \($0)")
}
obj2 = Test() //Create new instance.
withUnsafePointer(to: &obj2) {
    print(" var obj2 is still located at address: \($0)")
}
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0
如果要比较
obj1
obj2
中包含的引用,使用
ObjectIdentifier
将是一种简单的方法:

obj2 = Test.sharedInstance
print(" ObjectIdentifier of obj1 is \(ObjectIdentifier(obj1))")
print(" ObjectIdentifier of obj2 is \(ObjectIdentifier(obj2))")
obj2 = Test()
print(" ObjectIdentifier of obj2 is now \(ObjectIdentifier(obj2))")
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0
或者,您可以将引用转换为不透明指针,然后再转换为int,如下所示:

let addrInt1 = Int(bitPattern: Unmanaged.passRetained(obj1).toOpaque())
print("Address of obj1 = \(String(format: "%016lX", addrInt1))")
let addrInt2 = Int(bitPattern: Unmanaged.passRetained(obj2).toOpaque())
print("Address of obj2 = \(String(format: "%016lX", addrInt2))")
输出:

 var obj1 is located at address: 0x00000001003e4398
 var obj2 is located at address: 0x00000001003e43a0
 var obj2 is still located at address: 0x00000001003e43a0
 ObjectIdentifier of obj1 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is ObjectIdentifier(0x0000000100e04a80)
 ObjectIdentifier of obj2 is now ObjectIdentifier(0x0000000100e075f0)
Address of obj1 = 0000000100E04A50
Address of obj2 = 0000000100E049E0

我知道了,非常感谢!我知道了,非常感谢!