swift 打印引用类型地址,值类型地址

swift 5.x 版本

类型 打印
refrence type引用类型 print(Unmanaged.passUnretained(testType).toOpaque()) 替换testType为你的引用类型
object instance,metatype print(ObjectIdentifier(testType)) testType 为你的类型
value type 值类型 print(UnsafePointer(&testType))替换testType 为你的类型
struct PersonS {
}

class Friend: CustomStringConvertible{
    var name: String = "Friend"
    var description: String {
        return "name = \(name)"
    }
}

class Person: CustomStringConvertible {
    var age: Int = 20
    var friend: Friend = Friend()
    
    var description: String {
        return "age = \(age) friend: + \(friend.description)"
    }
}

let lee = Person()
lee.friend.name = "lee"
let han = lee
han.friend.name = "lee"
han.age = 21

print(lee)
print(han)

var ps = PersonS()
// 引用类型
print(Unmanaged.passUnretained(han).toOpaque()) //打印引用类型
print(Unmanaged.passUnretained(lee).toOpaque()) //打印引用类型
print(UnsafePointer(&ps)) //打印地址 值类型

//ObjectIdentifier 支持实例,元类型,不支持 enum, struct,func,tuple
print(ObjectIdentifier(Person.self)) // 打印 元类型
print(ObjectIdentifier(han)) // 打印 对象实例
print(ObjectIdentifier(han.self)) // 打印 对象实例

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容