ios - 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
无关,如下面的两组代码所示。internal
private
@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
强制执行的代码window
是internal
和不是private
,为什么当我更改为时它不适用door
?
解决方案
推荐阅读
- spring-boot - Spring-Cloud-Stream-Kafka 自定义健康检查不起作用
- xml - 渲染 gx:使用 Cesium 跟踪 KML 数据
- python - 使用 numpy.ctypeslib.load_library 时出错
- swift - 为什么这个开关不是详尽无遗的?
- javascript - React-datepicker 延迟打开 onClick
- php - afterFind 上的多个字段
- twilio - Twilio - 将自定义参数传递给 JS 客户端,以便我的客户端知道谁在调用
- c++ - 通过 <...> 访问大型 C++ 项目中的头文件
- javascript - 脚本在多列循环上运行如何只回发一次,而不是每列
- visual-studio - IIS Express 告诉我一个端口正在使用中,但 netstat 另有说明