You mean lines 11 to 15 I guess ?
window?.rootViewController is nil, because window is nil.
See here:
https://stackoverflow.com/questions/34159160/why-is-appdelegate-swift-window-an-optional
In some cases (for instance if you define your App as a Master Detail App), system handles initialization by itself.
In other cases, the template for the app does not even contain a window in AppDelegate. That's the case if you have a scene delegate
So, if you have SceneDelegate, remove the window declaration in AppDelegate, remove the code you added in didFinishLaunching and put it in Scenedelegate (you have a window declared here).
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
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 as? UIWindowScene) else { return }
print(window!) // Let's take the risk to unwrap !
print(window!.rootViewController!)
if let tabBarController = window?.rootViewController as? UITabBarController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
print("I passed here in willConnectTo")
let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
tabBarController.viewControllers?.append(vc)
}
}
// Other SceneDelegate func here
}
You will get on console:
<UIWindow: 0x7fc53640c110; frame = (0 0; 414 896); hidden = YES; gestureRecognizers = <NSArray: 0x6000030b90e0>; layer = <UIWindowLayer: 0x600003eaad00>>
<UITabBarController: 0x7fc536822200>
I passed here in willConnectTo
Good tutorial here:
h ttps://learnappmaking.com/scene-delegate-app-delegate-xcode-11-ios-13/