Swift类型信息

我们的程序加载到内存中,Swift中每个类型的信息都有一块对应内存存储着。


AnyObject Any AnyClass XXX.type_swift

获取这个内存地址



// 通过类型获取
Int.self
Double.self
Person.self

// 通过类型实例获取
type(of: 类型变量/实例)
print(Person.self === type(of: person)) // true

let num = 10
print(Int.self == type(of: num))


指向类型信息变量的类型 XXX.type



let intType: Int.Type = Int.self
let personType: Person.Type Person.self