swift 类型变量、元类型、类型

元类型的声明:

let x:String.Type

类型的获取:

type(of)、SomeBaseClass.self

 

let someInstance: SomeBaseClass = SomeSubClass()

                |                |

     compileTime       Runtime

                |                | 

To extract, use:       .self                type(of)

    对象    类          类的元类型

        type(of:someInstance)        type(of:SomeBaseClass)

 

类型的表达:SomeBaseClass、SomeBaseClass.Type、SomeBaseClass.Type

变量的表达: someInstance、type(of:someInstance)==SomeBaseClass.self、type(of:SomeBaseClass)

 

print(String.self)

print(type(of: greeting))

print(type(of: type(of: greeting)))

print(type(of: type(of: type(of: greeting))))

 

String

String

String.Type

String.Type.Type

 

print(String.self is String.Type) == true

posted @ 2025-09-01 18:45  zzfx  阅读(5)  评论(0)    收藏  举报