[英]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.我正在使用 MBProgressHUD 来显示加载程序,并且在访问应用程序窗口时,值为 nil 并且应用程序崩溃。 I already imported the MBProgressHUD and created a variable appDelgate equals to UIApplication.shared.delegate as!我已经导入了 MBProgressHUD 并创建了一个变量 appDelgate 等于 UIApplication.shared.delegate as! AppDelegate, and when accessing that variable window! AppDelegate,并在访问该变量窗口时! I get the follwing error: Fatal error: Unexpectedly found nil while unwrapping an Optional value我收到以下错误:致命错误:在展开可选值时意外发现 nil
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?你必须添加 var window: UIWindow? property in AppDelegate.swift. 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 如果应用程序委托要快速使用主故事板文件,则它必须实现 window 属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.