I try to create an instance of RootViewModel once by injecting it in the RootViewController when the app launches, in the didFinishLaunchingWithOptions of AppDelegate.swift so it doesn't get created multiple times.
Here's the code snippet:
...
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
}
...
The RootViewModel is a basic swift class with no implementation yet, and the RootViewController has an optional viewModel property to allow for the injection.
var viewModel: RootViewModel?
Here's my issue: Each time I run the app, it stops at the fatalError which I created to know if everything went well with the creation of the rootViewController. So, it means everything didn't go well.
I think the window property is still null at time of creating the rootViewController, but I am not sure how to resolve this.
I have tried creating the same thing in the SceneDelegate with no success.
What can I do to resolve this issue? I am using XCode Version 12.5