gpt4 book ai didi

swift - 使用变量类型进行类型转换

转载 作者:可可西里 更新时间:2023-11-01 02:04:20 25 4
gpt4 key购买 nike

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

我已经试过了:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com