如何在Swift中使用变量进行类型转换

5

我想尝试根据变量来转换类型。下面的示例失败并显示“未声明类型'myType'”

protocol Parent {}
protocol Child: Parent {}

struct Foo: Child {}

let foo: Parent = Foo()

let myType = Child.self

if let _ = foo as? myType {
    print("success")
}

有没有一种基于变量myType进行投射的方法?
1个回答

1

经过反思,这里不是问casting是否正确的问题。我只是在检查符合性。动态casting是无用的,因为静态分析器无法知道如果基于变量类型进行casting时该怎么做。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接