gpt4 book ai didi

ios - NSClassFromString 对于 Bundle.main.infoDictionary 返回 nil

转载 作者:行者123 更新时间:2023-11-30 11:59:59 26 4
gpt4 key购买 nike

我正在以编程方式实现 UITabBarController,但在尝试从 Bundle.main.infoDictionary 检索 Controller 时出现错误!["CFBundleExecutable"]

这是 UITabBarController 的代码:

import UIKit

class ENTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

addChildViewControllers()
}

fileprivate func addChildViewControllers() {
addChildViewController("ENSwipeViewController", imageName: "TabBar_swipe_")
addChildViewController("ENSocialViewController", imageName: "TabBar_social_")
addChildViewController("ENNotificationViewController", imageName: "TabBar_notification_")

}

fileprivate func addChildViewController(_ childControllerName: String, imageName: String) {
let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String

let cls: AnyClass? = NSClassFromString(ns + "." + childControllerName)

let vcClass = cls as! UIViewController.Type
let vc = vcClass.init()


vc.tabBarItem.image = UIImage(named: imageName)
vc.tabBarItem.selectedImage = UIImage(named: imageName + "selected")

let nav = UINavigationController()
nav.addChildViewController(vc)
addChildViewController(nav)
}
}

错误发生在let vcClass = cls as! UIViewController.Type 是:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

这是 ENSwipeViewController 的简单代码:

import UIKit

class ENSwipeViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .red
}
}

最佳答案

好吧,如果你看一下documentation你会看到它说:

Return Value

The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

因此,由于您的 ENSwipeViewController 尚未加载,因此它返回 nil...

关于ios - NSClassFromString 对于 Bundle.main.infoDictionary 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362148/

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