是否可以将对象转换为存储在成员变量中的类型?
我已经试过了:
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’”
最佳答案
没有。类型转换是编译时的事情。它让编译器知道哪些方法可用以及如何进行链接。
您可以使用 Mirror
在运行时收集有关您的对象的信息,但看起来动态调用方法仍然只能在从 NSObject
派生的类上完成。
关于swift - 使用变量类型进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44386754/