iOS - missing ‘window’ variable in AppDelegate

问题描述

在学习 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! ItemsViewController
        itemController.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值