Error: Missing return in a closure expected to return 'UIViewController' (Xcode, Swift, iOS 13)

本秂侑毒 提交于 2020-01-14 07:01:11

问题


I'm getting the error: "Missing return in a closure expected to return 'UIViewController'" on the bolded line. How can I fix this? Thank you!!

   Var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()


let hasSession = UserDefaults.standard.value(forKey: "UserHasSubmittedPassword") as? Bool
     let vc: UIViewController = {
               if let hasSession = hasSession, hasSession == true {
                    // next vc you want to show
                } else {
                    // enter password vc
                }
      **}()**

      let navigationController = UINavigationController(rootViewController: vc)
      window?.rootViewController = navigationController
      window?.makeKeyAndVisible()
      return true


    }

回答1:


you just need to return ViewController inside closure

Var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()

let Myvc = UIViewController()

let hasSession = UserDefaults.standard.value(forKey: "UserHasSubmittedPassword") as? Bool
     let vc: UIViewController = {
               if let hasSession = hasSession, hasSession == true {
                    // next vc you want to show
                       return Myvc
                } else {
                    // enter password vc
                        return Myvc
                }
      **}()**

      let navigationController = UINavigationController(rootViewController: vc)
      window?.rootViewController = navigationController
      window?.makeKeyAndVisible()
      return true
    }


来源:https://stackoverflow.com/questions/59495781/error-missing-return-in-a-closure-expected-to-return-uiviewcontroller-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!