如何仅在 iOS 13.0 之前包含此 swift 声明

How can I include this swift declaration only before iOS 13.0

我的 AppDelegate.swift 文件中有以下声明。

var window: UIWindow?

我只想在部署目标设置为低于 iOS 13.0 时包含它。我看过

@available(...)

属性,但我不认为它可以做我想做的事。本质上,我希望能够为 iOS 12 或 iOS 13 构建我的项目,并且在 AppDelegate.swift 和 SceneDelegate.swift 中对我的项目进行最少的更改。我已经使用 @available(iOS 13.0, *)#available(iOS 13.0, *) 对 AppDelegate.swift 和 SceneDelegate.swift 进行了更改,但是当部署目标高于 [=37 时,我仍然需要能够排除其他声明=] 12.

这是我的 AppDelegate.swift 文件:

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow? // only include for builds < iOS 13.0
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {                          
        if #available(iOS 13.0, *) {
            // Do nothing
        } else {     
            window = UIWindow() // only include for builds < iOS 13.0
            window?.rootViewController = UINavigationController(rootViewController: LoginController()) // only include for builds < iOS 13.0
            window?.makeKeyAndVisible() // only include for builds < iOS 13.0      
        } 
        FirebaseApp.configure()
        return true
    }
    // MARK: UISceneSession Lifecycle
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

这是我的 SceneDelegate.swift 文件:

import UIKit

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: scene)
        window?.rootViewController = UINavigationController(rootViewController: LoginController())
        window?.makeKeyAndVisible()
    }
    // ...
}

基本上我想要的只是更改部署目标,然后在构建过程中包含或排除适当的代码位。

var window: UIWindow? // only include for builds < iOS 13.0

不要管声明。在 iOS 12 及之前,它将引用应用程序 window。在 iOS 13 及以后它将为零并且不会造成任何伤害。