Returns the dynamic type of a value.
SDK
- Xcode 9.0+
Framework
- Swift Standard Library
Declaration
Parameters
value
The value for which to find the dynamic type.
Return Value
The dynamic type, which is a metatype instance.
Discussion
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 a subtype of its concrete type.
In the following code, the count
variable has the same static and dynamic type: Int
. When count
is passed to the print
function, however, the value
parameter has a static type of Any
(the type declared for the parameter) and a dynamic type of Int
.
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 print
function in the example below is an instance of 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 Smiley
metatype.
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 P
.
The following example defines a print
function that takes a generic parameter and declares the String
type’s conformance to a new protocol P
. When print
is called with a string that has P
as its static type, the call to type(of:)
returns P
instead of String
(the dynamic type inside the parameter).
This unexpected result occurs because the call to type(of: value)
inside print
must return a metatype that is an instance of T
, but 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 type(of:)
.