Returns the dynamic type of a value.
- Xcode 9.0+
- Swift Standard Library
The value for which to find the dynamic type.
The dynamic type, which is a metatype instance.
You can use the
type(of:) function to find the dynamic type of a value, particularly when the dynamic type is different from the static type. The static type of a value is the known, compile-time type of the value. The dynamic type of a value is the value’s actual type at run-time, which can be nested inside its concrete type.
In the following code, the
count variable has the same static and dynamic type:
count is passed to the
value parameter has a static type of
Any (the type declared for the parameter) and a dynamic type of
The dynamic type returned from
type(of:) is a concrete metatype (
T) for a class, structure, enumeration, or other nonprotocol type
T, or an existential metatype (
P) for a protocol or protocol composition
P. When the static type of the value passed to
type(of:) is constrained to a class or protocol, you can use that metatype to access initializers or other static members of the class or protocol.
For example, the parameter passed as
value to the
Smiley class or one of its subclasses. The function uses
type(of:) to find the dynamic type of
value, which itself is an instance of the
In this example, accessing the
text property of the
smiley metatype retrieves the overridden value from the
Emoji subclass, instead of the
Smiley class’s original definition.
Finding the Dynamic Type in a Generic Context
Normally, you don’t need to be aware of the difference between concrete and existential metatypes, but calling
type(of:) can yield unexpected results in a generic context with a type parameter bound to a protocol. In a case like this, where a generic parameter
T is bound to a protocol
P, the type parameter is not statically known to be a protocol type in the body of the generic function. As a result,
type(of:) can only produce the concrete metatype
The following example defines a
String type’s conformance to a new protocol
P as its static type, the call to
P instead of
String (the dynamic type inside the parameter).
This unexpected result occurs because the call to
type(of: value) inside
String (the expected dynamic type) is not an instance of
P (the concrete metatype of
value). To get the dynamic type inside
value in this generic context, cast the parameter to
Any when calling