如何正确地 属性 在 AppDelegate.swift 中注入对象

How to properly do property injection of an object in AppDelegate.swift

我尝试创建一个 RootViewModel 实例,方法是在应用程序启动时将其注入 RootViewController,在 didFinishLaunchingWithOptions of AppDelegate.swift 所以它不会被创建多次。

这是代码片段:

...

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        guard let rootViewController = window?.rootViewController as? RootViewController else {
            fatalError("Unable to Instantiate the root view controller")
        }

        let rootViewModel = RootViewModel()
        
        rootViewController.viewModel = rootViewModel
        
        return true
    }

...

RootViewModel 是一个基本的 swift class,还没有实现,RootViewController 有一个可选 viewModel 属性 以允许注入。

var viewModel: RootViewModel?

这是我的问题:每次我 运行 应用程序时,它都会在我创建的 fatalError 处停止,以了解创建 rootViewController。所以,这意味着一切都不顺利。

我认为 window 属性 在创建 rootViewController 时仍然为空,但我不确定如何解决这个问题。

我尝试在 SceneDelegate 中创建相同的东西,但没有成功。

我该怎么做才能解决这个问题?我正在使用 XCode 版本 12.5

由于您采用了新的场景生命周期并拥有场景委托,因此您需要在willConnectTo场景委托函数中访问根视图控制器。

这是因为您的应用可能不再有单个 window,但可能有多个 windows,例如,如果您在 iPadOS 上支持多个视图。


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
     guard let scene = (scene as? UIWindowScene) else { return }
        
     if let rootVC = scene.windows.first?.rootViewController as? RootViewController {
         rootViewController.viewModel = RootViewModel()
    }
}