BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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?

   

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

   

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 }
  }
 }

   

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

   

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.

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.