首页 > 解决方案 > UIApplicationDelegate 如何强制其“window”为“internal”而不是“private”,但是当名称更改为“door”时可以吗?

问题描述

我正在制作一个没有故事板的 iOS 应用程序,如下所示。我需要手动分配window?.rootViewController等。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    internal var window: UIWindow? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = .white
        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
        return true
    }
}

查看代码,我认为因为window不是覆盖,所以我可以这样做;即使window非可选。但是下面的代码不起作用。显然,window需要成为AppDelegate成员的一部分。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let window = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = .white
        window.rootViewController = viewController
        window.makeKeyAndVisible()
        return true
    }
}

有了这个,我然后尝试下面与我的第一个代码相同的内容,除了window现在是private而不是internal. 我有投诉Property 'window' must be as accessible as its enclosing type because it matches a requirement in protocol 'UIApplicationDelegate'

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    private var window: UIWindow? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = .white
        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
        return true
    }
}

我尝试查看 的代码UIApplicationDelegate,但无法找出哪些代码window需要公开。

令我感兴趣的是,如果我将window名称更改为另一个名称,例如,它会与ordoor无关,如下面的两组代码所示。internalprivate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    internal var door: UIWindow? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        door = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = .white
        door?.rootViewController = viewController
        door?.makeKeyAndVisible()
        return true
    }
}

door它也适用private于以下情况。没有投诉。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    private var door: UIWindow? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        door = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        viewController.view.backgroundColor = .white
        door?.rootViewController = viewController
        door?.makeKeyAndVisible()
        return true
    }
}

所以我很好奇,UIApplicationDelegate强制执行的代码windowinternal和不是private,为什么当我更改为时它不适用door

标签: iosswift

解决方案


推荐阅读