gpt4 book ai didi

ios - 为什么将 UIApplicationDelegate 中的 window 属性设为可选而不是隐式解包可选?

转载 作者:可可西里 更新时间:2023-11-01 01:28:04 24 4
gpt4 key购买 nike

为什么 var window: UIWindow? 而不是 UIWindow!,也就是说,为什么让 window 成为可选的,不是每个应用程序都应该有一个主 UIWindow(没有它,应用程序不能'根本没有功能)?!

我看到了this answer说它确实从 UIWindow! 变成了 UIWindow? 所以它一定是有原因的。我只是不明白为什么。还有,有了UIWindow?,主窗口的类型变成了double optional,真是别扭。

最佳答案

这是因为将 UIMainStoryboardFile 键添加到应用程序的 .plist 文件(默认添加)是实例化窗口的原因。

enter image description here

window rootViewController 将包含 Storyboard的初始 View Controller ,它将在屏幕上可见。

测试一下!

我们可以让 window 为零吗?

尝试从项目的 .plist 文件中删除 UIMainStoryboardFile 行,并尝试在 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) 方法:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// if your removed 'UIMainStoryboardFile' key from the plist, this should prints nil
print(window)

return true
}

关于ios - 为什么将 UIApplicationDelegate 中的 window 属性设为可选而不是隐式解包可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com