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

SOLVED: Is the code in Finishing touches: didFinishLaunchingWithOptions (Day 34, Task 2) out of date?

Forums > 100 Days of Swift

The tutorial asks you to add some code to the didFinishLaunchingWithOptions function in AppDelegate.swift. Unfortunately the code doesn't work as there is no window variable.

if let tabBarController = window?.rootViewController as? UITabBarController { //error: Cannot find 'window' in scope

Is it as simple as declaring var window: UIWindow? as a member variable to the AppDelegate class?

3      

Adding the member variable to AppDelegate does not work, although it does compile. No new tab is displayed in the UI.

3      

Hi there! Yes, structure has been changed since then. Now it is in SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

  var window: UIWindow?

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

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

    // This creates second tab bar item
//    if let tabBarController2 = window?.rootViewController as? UITabBarController {
//      let storyboard = UIStoryboard(name: "Main", bundle: nil)
//      let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
//      vc.tabBarItem = UITabBarItem(tabBarSystemItem: .mostViewed, tag: 2)
//      tabBarController2.viewControllers?.append(vc)
//    }

    guard let _ = (scene as? UIWindowScene) else { return }
  }
 }

3      

Thanks for clearing that up. Can the tutorial be updated for future students?

3      

Well this is good question for Paul :) But since all attention now on SwiftUI framework, I wouldn't be so sure that it is going to be updated soon. But nevertheless his course is not so really oudated as others. There are some other places that unfortunately will make your brain hurt :) But nevertheless solvable.

3      

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.