我正在创建一个 iOS 应用程序。我遇到了一个问题:标识符“window”未解析。
这可能是因为我使用的是 Swift 的测试版和 Xcode 的测试版,但我需要它们来开发这个应用程序。我尝试搜索其他问题,但没有回答有关标识符“窗口”的问题。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard let splitViewController = window?.rootViewController as? UISplitViewController,
let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
let masterViewController = leftNavController.topViewController as? MasterViewController,
let detailViewController = splitViewController.viewControllers.last as? DetailViewController
else { fatalError() }
let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster
return true
}
我希望最终能够选择一个菜单项并在另一个 View Controller 上显示详细信息,但我收到了一个带有未解析标识符的错误。
最佳答案
This may be because I'm using the beta version of Swift and beta version of Xcode
在 iOS 13 中,因此在 Xcode 11 下创建的任何新项目中,window
属性已从应用程序委托(delegate)中移出,现在驻留在场景委托(delegate)中。
https://developer.apple.com/documentation/uikit/uiscenedelegate
window
属性并不是 UISceneDelegate 协议(protocol)的正式组成部分,但它是 UIWindowSceneDelegate 协议(protocol)的一部分:
https://developer.apple.com/documentation/uikit/uiwindowscenedelegate
在新架构中,窗口场景委托(delegate)实例是界面创建和维护的所在地,包括窗口的访问、其rootViewController
的配置等。项目模板现在包含一个文件 SceneDelegate.swift,其中包含符合 UIWindowSceneDelegate 的 SceneDelegate 类的声明;这是样板代码的结构:
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
}
func sceneWillResignActive(_ scene: UIScene) {
}
func sceneWillEnterForeground(_ scene: UIScene) {
}
func sceneDidEnterBackground(_ scene: UIScene) {
}
}
因此,您的界面创建代码应移至场景委托(delegate)的 scene(_:willConnectTo:options:)
实现中。
https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene
您还需要将任何Active
或Foreground
/Background
代码从应用程序委托(delegate)移出并移入场景委托(delegate)。您应该观看 iOS 13 下的多窗口/多任务处理的 WWDC 2019 视频,并阅读文档:
https://developer.apple.com/documentation/uikit/app_and_scenes
关于swift - 'window' 不是 Swift iOS 开发中的标识符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611247/