【问题标题】:How do you cast in swift using a variable for the type你如何使用类型的变量快速转换
【发布时间】:2015-11-30 16:23:27
【问题描述】:

我正在尝试根据变量强制转换类型。下面的示例失败说“使用未声明的类型,'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进行强制转换?

【问题讨论】:

    标签: swift casting


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多