1
votes

newbie in UIKit/SwiftUI here, now I'm working on a SwiftUI App with UIKit Life Cycle and I got this problem

Cannot find type 'NSPersistentCloudKitContainer' in scope

The code is that:

import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        //nothing
    }

    lazy var persistentContainer: NSPersistentCloudKitContainer = { **Here is problem**
        let container = NSPersistentCloudKitContainer(name: "app_Swiftui")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

}

I get Build Failed everytime and I get this error. Can somebody help me?

2
I don't think that NSPersistendCloudKitContainer is in Firebase. It's actually in CloudKit (Apple's own storage). import CloudKit and see how it works. - goatofanerd
same problem, 'Cannot find type 'NSPersistentCloudKitContainer' in scope' and i imported CloudKit. - Razvan Constantin
Try CoreData. Double checked the documentation and it seems like a CoreData - goatofanerd
That's it man! Thank you so much! Post answer here and let me vote your answer! - Razvan Constantin
Done! Thank you very much. - goatofanerd

2 Answers

4
votes

NSPersistendCloudKitContainer is a CoreData class.

import CoreData and you'll be on your way!

1
votes

Here you go:
NSPersistentCloudKitContainer is a subclass of NSPersistentContainer capable of managing both CloudKit-backed and noncloud stores.

So its a Core Data!

Declarer it on top:

import CoreData

For more details read this page Apple Doc also this can help Apple Doc2