【问题标题】:Type casting with type in variable使用变量中的类型进行类型转换
【发布时间】:2017-06-06 09:51:36
【问题描述】:

是否可以将对象转换为存储在成员变量中的类型?

我试过了:

let targetClass = type(of: MyTargetClass)
...

if anyObject is targetClass { // ERROR: use of undeclared type 'targetClass'
let test = anyObject as! targetClass // ERROR: use of undeclared type 'targetClass'
}

但它不起作用,因为它说“使用未声明的类型'targetClass'”

【问题讨论】:

    标签: swift swift3 casting


    【解决方案1】:

    没有。类型转换是编译时的事情。它让编译器知道哪些方法可用以及如何进行链接。

    您可以使用Mirror 在运行时收集有关您的对象的信息,但看起来动态调用方法仍然只能在派生自NSObject 的类上完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多