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

I cannot change my existing core data stack to NSPersistentCloudKitContainer because that is only a iOS 13 and later support feature but I would like to have that set for iOS 13 and later while having NSPersistentContainer for earlier versions.

I have a decent amount of experience working with swift and iOS but have not ever had to do something like this for versioning and certain vars/lets on certain verions.


This is my code for my coredata stack for the iOS 13 and later for NSPersistentCloudKitContainer:

     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
}()


This is my code for iOS 12 and earlier:

lazy var persistentContainer: NSPersistentContainer = {


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

}()


How could I keep both of these but have the first for iOS 13 and later and the second for iOS 12 and earlier? I would like to have one var for both if that is even possible and if that is not possible I would like a easy solution that makes it so I do not have to rewrite all that I have already to include two different versions and vars.

Answered by OOPer in 375937022

Why don't you use `if #available` ?

    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
        }
    }()
Accepted Answer

Why don't you use `if #available` ?

    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
        }
    }()

That worked purfectly, thank you so much! I honestly did not try that becouse I did not think it would work with line one and three in that block of code. I did not know that even though you set persistentContainer with type of NSPersistentContainer that you could alter that with type NSPersistentCloudKitContainer on line three. I will have to keep that in mind for the furture, thank you.

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