Return the dynamic type of a value.
The type(of:) function returns the metatype of the dynamic (runtime) type of a value. This is useful for logging, debugging, or working with generic code where the static type may differ from the actual runtime type.
For a class instance, type(of:) returns the actual subclass type, not the declared superclass type. For protocol-typed values, it returns the concrete conforming type.
The returned metatype can be used with initializers to create new instances of the same type, or with String(describing:) to get the type name as a string.
| Name | Description | Optional |
|---|---|---|
value |
The value whose dynamic type is to be returned. | No |
let number = 42
print(type(of: number)) // Int
let text: Any = "hello"
print(type(of: text)) // String