当前位置: 代码迷 >> 综合 >> iOS - missing ‘window’ variable in AppDelegate
  详细解决方案

iOS - missing ‘window’ variable in AppDelegate

热度:42   发布时间:2023-09-21 23:15:29.0

问题描述

在学习 iOS 编程 UITableView 数据源 时,让控制器访问 ItemStore 报错
missing ‘window’ variable in AppDelegate

Xcode 版本: 12.3
iOS 版本: 14.3
Language: swift
模拟器型号: iPhone 8 plus

解决方法

SceneDelegate.swift 中创建 ItemStore 而不要在 AppDelegate.swift 中创建

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let itemStore = ItemStore()let itemController = window!.rootViewController as! ItemsViewControlleritemController.itemStore = itemStore// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).guard let _ = (scene as? UIWindowScene) else {
     return }}

参考资料:
Swift 4 and Xcode 11: Missing ‘window’ variable

Project 7 - Missing ‘window’ variable in AppDelegate

App Delegate - UIWindow is not working properly

  相关解决方案