如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?

How do I create a variable for NSPersistentContainer and NSPersistentCloudKitContainer with the same name but set depending on the version?

我无法将令人兴奋的核心数据堆栈更改为 NSPersistentCloudKitContainer,因为这只是 iOS 13 及更高版本的支持功能,但我希望为 iOS 13 及更高版本设置该功能,同时为 NSPersistentContainer早期版本。

我在使用 swift 和 iOS 方面有相当多的经验,但从来没有为版本控制和某些变量做这样的事情。

这是我在使用 iOS 13 之前的正常工作:

lazy var persistentContainer: NSPersistentContainer = {
    /*
     The persistent container for the application. This implementation
     creates and returns a container, having loaded the store for the
     application to it. This property is optional since there are legitimate
     error conditions that could cause the creation of the store to fail.
    */
    let container = NSPersistentContainer(name: "Shopping_App")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

这是我想在 iOS 13 或更高版本的任何设备上使用的内容,但仍然使用 iOS 12 及更早版本的代码的第一位。

lazy var persistentContainer: NSPersistentCloudKitContainer = {

    let container = NSPersistentCloudKitContainer(name: "Shopping_App")
    container.loadPersistentStores(completionHandler: {
        (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

如果可能的话,我希望两个版本使用相同的变量而不重新声明错误,如果这可能的话,如果那不可能的话,我想要一个不需要完全重写所有内容的解决方案我存储和检索核心数据 data.

我找到了答案,结果非常简单。这是:

lazy var persistentContainer: NSPersistentContainer = {  
    if #available(iOS 13.0, *) {  
        let container = NSPersistentCloudKitContainer(name: "Shopping_App")  
        container.loadPersistentStores(completionHandler: {  
            (storeDescription, error) in  
            if let error = error as NSError? {  
                fatalError("Unresolved error \(error), \(error.userInfo)")  
            }  
        })  
        return container  
    } else {  
        let container = NSPersistentContainer(name: "Shopping_App")  
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in  
            if let error = error as NSError? {  

                fatalError("Unresolved error \(error), \(error.userInfo)")  
            }  
        })  
        return container  
    }  
}()