Can you cast to a Metatype Type in Swift? It really seems like you should be able to (after all you can instantiate objects from Metatypes).
你能在Swift中转换为元类型吗?看起来你应该能够(毕竟你可以从Metatypes实例化对象)。
The following doesn't work:
以下不起作用:
class Super {}
class A : Super {}
let superA:Super = A()
let subType = superA.dynamicType
let afterCast = superA as subType
//Compiler error: "use of undeclared type 'subType'"
Does anyone know the right way to do this?
有谁知道正确的方法吗?
Edit:
编辑:
As newacct pointed out, the result of .dynamicType is obviously not known until runtime, so a compile-time cast to the result of .dynamicType would not make sense.
正如newacct指出的那样,直到运行时才明白.dynamicType的结果,因此编译时转换为.dynamicType的结果是没有意义的。
So the answer is: "You can't" (and there is no good reason to try).
所以答案是:“你不能”(并且没有充分的理由去尝试)。
1 个解决方案
#1
4
First of all, as
takes a type, not an expression, on the right-hand side. So what you have is a syntax error.
首先,在右侧采用类型而不是表达式。所以你有一个语法错误。
What you seem to be trying to do is "cast" to a type that is computed at runtime. What would that even mean? Let's first consider what is a "cast".
您似乎要尝试做的是“强制转换”为在运行时计算的类型。这甚至意味着什么?让我们首先考虑什么是“演员”。
Usually, when we have a cast expression x as T
, it has two components:
通常,当我们将转换表达式x作为T时,它有两个组件:
- At compile-time: The entire cast expression
x as T
has compile-time typeT?
, which allows you to do stuff with the resulting expression that you maybe cannot do onx
directly. In other words, it allows you to change the compile-time type. - 在编译时:作为T的整个强制转换表达式x具有编译时类型T ?,它允许您使用您可能无法直接在x上执行的结果表达式执行操作。换句话说,它允许您更改编译时类型。
- At runtime: It checks whether the runtime type of
x
is a subtype ofT
, and if it is, it evaluates to the optional containing that value, otherwise, it evaluates tonil
. - 在运行时:它检查x的运行时类型是否是T的子类型,如果是,则计算为包含该值的可选项,否则,计算结果为nil。
If the type T
is not known at compile-time, then obviously you cannot do the compile-time part of it. (The compile-time type of the resulting expression cannot, obviously, depend on something which is not known at compile-time.)
如果在编译时不知道类型T,那么显然你不能做它的编译时部分。 (结果表达式的编译时类型显然不能依赖于编译时未知的内容。)
The other part, the runtime component, could that be done with a type computed at runtime? Sure. For example,
另一部分,即运行时组件,可以使用在运行时计算的类型来完成吗?当然。例如,
import Foundation
let afterCast : Super? =
(superA as AnyObject).isKindOfClass(subType) ? superA : nil
It's not clear if that is what you want.
目前尚不清楚这是否是你想要的。
#1
4
First of all, as
takes a type, not an expression, on the right-hand side. So what you have is a syntax error.
首先,在右侧采用类型而不是表达式。所以你有一个语法错误。
What you seem to be trying to do is "cast" to a type that is computed at runtime. What would that even mean? Let's first consider what is a "cast".
您似乎要尝试做的是“强制转换”为在运行时计算的类型。这甚至意味着什么?让我们首先考虑什么是“演员”。
Usually, when we have a cast expression x as T
, it has two components:
通常,当我们将转换表达式x作为T时,它有两个组件:
- At compile-time: The entire cast expression
x as T
has compile-time typeT?
, which allows you to do stuff with the resulting expression that you maybe cannot do onx
directly. In other words, it allows you to change the compile-time type. - 在编译时:作为T的整个强制转换表达式x具有编译时类型T ?,它允许您使用您可能无法直接在x上执行的结果表达式执行操作。换句话说,它允许您更改编译时类型。
- At runtime: It checks whether the runtime type of
x
is a subtype ofT
, and if it is, it evaluates to the optional containing that value, otherwise, it evaluates tonil
. - 在运行时:它检查x的运行时类型是否是T的子类型,如果是,则计算为包含该值的可选项,否则,计算结果为nil。
If the type T
is not known at compile-time, then obviously you cannot do the compile-time part of it. (The compile-time type of the resulting expression cannot, obviously, depend on something which is not known at compile-time.)
如果在编译时不知道类型T,那么显然你不能做它的编译时部分。 (结果表达式的编译时类型显然不能依赖于编译时未知的内容。)
The other part, the runtime component, could that be done with a type computed at runtime? Sure. For example,
另一部分,即运行时组件,可以使用在运行时计算的类型来完成吗?当然。例如,
import Foundation
let afterCast : Super? =
(superA as AnyObject).isKindOfClass(subType) ? superA : nil
It's not clear if that is what you want.
目前尚不清楚这是否是你想要的。