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 atrun-time
, which can be nested inside its concrete type
.
您可以使用type(of:) 函数来查找值的动态类型,尤其是当动态类型与静态类型不同时。 值的静态类型是值的已知编译时类型。 值的动态类型是运行时值的实际类型,可以嵌套在其具体类型中。
In the following code, the count variable has the same static and dynamic type
: Int
. When count is passed to the printInfo(_:) function, however, the value
parameter has a static type of Any
(the type declared for the parameter) and a dynamic type of Int
.
在以下代码中,count变量具有相同的静态和动态类型:Int。 但是,当count传递给printInfo(_:) 函数时,value参数的静态类型为Any(为参数声明的类型)和动态类型是Int。
func printInfo(_ value: Any) {
let type = type(of: value)
print("'\(value)' of type '\(type)'")
}
let count: Int = 5
printInfo(count)
// '5' of type 'Int'
The dynamic type
returned from type(of:) is a concrete metatype
(T.Type) for a class, structure, enumeration, or other nonprotocol type
T, or an existential metatype
(P.Type) 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.
从type(of:)返回的动态类型是用于类,结构,枚举或其他非协议类型T的具体元类型(T.Type),或用于协议或协议组合P的存在性元类型(P.Type)。 当传递给type(of :)的静态类型的值被约束为类或协议时,您可以使用该元类型来访问初始化程序或类或协议的其他静态成员。
For example, the parameter
passed as value to the printSmileyInfo(_:) 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.Type metatype.
例如,作为值传递给下面示例中的printSmileyInfo(_ :)函数的参数是Smiley类或其子类之一的实例。 该函数使用type(of:)来查找动态类型的值,它本身就是Smiley.Type元类型的一个实例。
class Smiley {
class var text: String {
return ":)"
}
}
class EmojiSmiley : Smiley {
override class var text: String {
return "😀"
}
}
func printSmileyInfo(_ value: Smiley) {
let smileyType = type(of: value)
print("Smile!", smileyType.text)
}
let emojiSmiley = EmojiSmiley()
printSmileyInfo(emojiSmiley)
// Smile! 😀
In this example, accessing the text property of the smileyType metatype retrieves the overridden value from the EmojiSmiley subclass, instead of the Smiley class’s original definition.
在此示例中,访问smileyType元类型的text属性将从EmojiSmiley子类中检索重写的值,而不是Smiley类的原始定义。
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.Protocol.
通常,您不需要了解具体和存在的元类型之间的区别,但是调用type(of:)会在具有绑定到协议的类型参数的泛型上下文中产生意外结果。 在这种情况下,在泛型参数T绑定到协议P的情况下,类型参数在静态上不是泛型函数体中的协议类型。 因此,type(of:)只能生成具体的元类型P.Protocol。
The following example defines a printGenericInfo(_:)
function that takes a generic parameter and declares the String type’s
conformance to a new protocol P
. When printGenericInfo(_:)
is called with a string that has P
as its static type
, the call to type(of:)
returns P.self instead of String.self (the dynamic type inside the parameter).
下面的示例定义了一个printGenericInfo(_ :)函数,该函数接受泛型参数并声明String类型遵守新协议P。当使用以P作为其静态类型的字符串调用printGenericInfo(_ :)时,调用type(of:)返回P.self而不是String.self(参数内的动态类型)。
func printGenericInfo<T>(_ value: T) {
let type = type(of: value)
print("'\(value)' of type '\(type)'")
}
protocol P {}
extension String: P {}
let stringAsP: P = "Hello!"
printGenericInfo(stringAsP)
// 'Hello!' of type 'P'
This unexpected result occurs because the call to type(of: value)
inside printGenericInfo(_:)
must return a metatype that is an instance of T.Type
, but String.self
(the expected dynamic type) is not an instance of P.Type (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:).
发生此意外结果是因为对printGenericInfo(_ :)中的type(of: value)的调用必须返回作为T.Type实例的元类型,但是
String.self
(预期的动态类型)不是P.Type的实例(值的具体元类型)。 要在此泛型上下文中获取动态类型内部值,请在调用type(of:)
时将参数强制转换为Any。
func betterPrintGenericInfo<T>(_ value: T) {
let type = type(of: value as Any)
print("'\(value)' of type '\(type)'")
}
betterPrintGenericInfo(stringAsP)
// 'Hello!' of type 'String'