简体   繁体   中英

Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value

I'm using MBProgressHUD to show a loader, and when acessing the app window the value in nil and the app crashes. I already imported the MBProgressHUD and created a variable appDelgate equals to UIApplication.shared.delegate as! AppDelegate, and when accessing that variable window! I get the follwing error: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Code:

import MBProgressHUD
let appDelgate = UIApplication.shared.delegate as! AppDelegate
MBProgressHUD.showAdded(to: appDelgate.window!, animated: true)

I cant seem to find what I missing here

You have to add var window: UIWindow? property in AppDelegate.swift. You can refer to this link

The app delegate must implement the window property if it wants to use a main storyboard file swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM