gpt4 book ai didi

ios - 实例化 ViewController 并将其从 Swift 中的字符串转换为类

转载 作者:行者123 更新时间:2023-11-28 12:15:16 25 4
gpt4 key购买 nike

我有一个字符串数组

var arr = ["firstViewController", "secondViewController", ....]

对于其中的每一项,我都需要实例化 ViewController

var myVC = self.storyboard?.instantiateViewController(withIdentifier: "firstViewController") as! firstViewController

如果我想从循环中实例化 View Controller ,我需要从字符串创建类,但是这段代码

let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
let cls: AnyClass = NSClassFromString("\(namespace).\(className)")!

不工作

最佳答案

在 Swift 中,您不能转换为运行时类型对象。 Swift 在编译时知道所有变量的类型。

现在你有一个变量,它的值是一些编译器不知道的类型,所以编译器不能让你对变量做任何类型转换。

引用:- Link1 , Link2

关于ios - 实例化 ViewController 并将其从 Swift 中的字符串转换为类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46845908/

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