Metatype
// metatype of a (class, struct, enum) type
SomeType.Type
// metatype of a protocol
SomeProtocol.Type// base class
class A {
class var className: String { "A" } // โญ๏ธ static member
}
// subclass
class B: A {
override class var className: String { "B" }
}
let obj: A = B() // โญ๏ธ compile-time type: A
// โญโโโโโโโโโโโโฎ โ โญ๏ธ runtime type as a value
type(of: obj).className // โญ๏ธ runtime type: B // parameter `value`
func printInfo(_ value: Any) { // โญ๏ธ static : Any
let t = type(of: value) // โญ๏ธ dynamic: Int
print("'\(value)' of type '\(t)'")
}
// constant `count`
let count: Int = 5 // static: Int, dynamic: Int (same)
printInfo(count)๐ Vector2D
iOS 15 Programming Fundamentals with Swift (2021) - Type Reference, p.187 โญ๏ธ
Last updated
Was this helpful?