你如何使用变量为swift转换类型

Tal*_*Tal 5 casting swift

我想尝试根据变量强制转换类型.下面的例子没有说"使用未声明的类型,'myType'"

protocol Parent {}
protocol Child: Parent {}

struct Foo: Child {}

let foo: Parent = Foo()

let myType = Child.self

if let _ = foo as? myType {
    print("success")
}
Run Code Online (Sandbox Code Playgroud)

有没有办法根据变量进行投射myType

Tal*_*Tal 1

经过反思,选角并不是在这里问的正确问题。我在这里只是检查一致性。动态转换将毫无用处,因为如果静态分析器基于变量类型进行转换,则无法知道如何处理它。