【发布时间】:2020-02-04 20:14:14
【问题描述】:
当我尝试使用“!”解开“var window”时它显示 Optional 值为 nil。
致命错误:在展开可选值时意外发现 nil
var window: UIWindow?
// image to be animated
let backgroundImg = UIImageView()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// creating imageView to store background image "mainbg.jpg"
backgroundImg.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
backgroundImg.image = UIImage(named: "mainbg.jpg")
self.window!.addSubview(backgroundImg)
return true
}
有其他人遇到过这个问题并设法解决了吗?
我正在使用带有 SWIFT 5 的 XCODE 版本 11.3.1。这个
【问题讨论】:
-
如果您不使用情节提要,则不会为您创建窗口,这就是为什么它可能是
nil。如果您使用的是故事板,那么您需要进行一些调试并将您的发现添加到您的问题中。
标签: swift xcode appdelegate swift5 uiwindow