UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 34, Project 7 Problem with window. in AppDelegate.swift (didFinishLAunchingWithOptions)

Forums > 100 Days of Swift

Hi All, I'm new(ish) to Swift, having learnt it to a rudimentary level about 4 years ago, and now following this excellent course to remind me of everything I have now completely forgotten. I seem to be following everything reasonably well so far, but the code in this exercise is causing me a compile error as below. Could be something I've buggered up somewhere else, but have been scratching my head trying various things with little success...

if let tabBarController = window?.rootViewController as? UITabBarController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
    vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
    tabBarController.viewControllers?.append(vc)
}

The first line of this is giving me compile error "Use of unresolved identifier 'window'" Any thoughts would be very much appreciated!

Cheers

3      

Try putting this code in your SceneDelegate instead of AppDelegate. Multi-window support in iOS 13 changed the way things are done and this kind of thing should be done in SceneDelegate now.

10      

Lovely job, that's sorted it! Cheers

4      

I ran into this also! After experimenting for a while, I found 2 ways to get the project to run!

First way to correct: In the appDelegate.swift file add this line below the class: "var window: UIWindow?" Now the project would run but the Top Rated button was not visible! To get the button to show: Open info.plist and delete the key "Application Scene Manifest".

The button does appear and works but I did not think this was the best solution!

Second way to make the button visible is the way @roosterboy pointed out. That is the way I decided to use.

5      

I also used @roosterboy's solution, and that resolved my problem. For what it's worth, I inserted the code in the scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) function.

6      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.