Swift Meta Type & .Self, & AnyClass
AnyClass
AnyClass
is no more than a type alias about AnyObject.Type
typealias AnyClass = AnyObject.Type
Meta Type
Meta type in Swift is some of backbone of Swift typing system. We could use A.Type
and A.self
.
A.Type
is equivalent to Type of Meta Type Instance and A.self
is Meta Type Instance. So we can code like that.
let typeInt: Int.Type = Int.self
Usage of Meta Type
This utensil of type system allows us to call initializer of type like that.
let a = typeInt.init("1")
or call class
or instance
method of type
class A {
required init() {}
class func method() {
print("Hello")
}
func method2() {
print("Hello2")
}
}
let typeA: A.Type = A.self
type.method()
typeA.method2(A())()
typeA.init() // needs required init
}
Comments