簡體   English   中英

限定Bundle.main.url使用核心數據為iOS9和上述夫特4

[英]Define Bundle.main.url to use Core Data for iOS9 and above Swift 4

我只是學習核心數據,我需要實現Core Data兩者iOS 9iOS 10作為我唯一的iPad測試設備是運行的iPad3 iOS 9.3.5 我試圖按照這種解決方案https://charleswilson.blog/2016/09/09/out-of-context-using-core-data-on-ios-9-3-and-10-0/ (不相信我可以粘貼鏈接整個代碼),因為我未能從堆棧溢出實施其他解決方案。 我不知道如果我得到了這樣一件事右:里面的lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator還有這let modelURL = Bundle.main.url(forResource: modelName, withExtension: modelExtension)! 我在其他的解決方案,在這里看到的堆棧溢出和他們都聲明為不同的String的值forResource參數,但都具有相同的"momd"withExtension:參數。 其實,我認為,因為我使用的是.xcdatamodeld我應該把我的數據模型名forResource參數和"xcdatamodeld"withExtension:參數,導致我的情況如下:

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let url = self.applicationDocumentsDirectory.appendingPathComponent("fix_it_shop").appendingPathExtension("xcdatamodeld")

        do {
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
        } catch {
            let dict : [String : Any] = [NSLocalizedDescriptionKey        : "Failed to initialize the application's saved data" as NSString,
                                         NSLocalizedFailureReasonErrorKey : "There was an error creating or loading the application's saved data." as NSString,
                                         NSUnderlyingErrorKey             : error as NSError]

            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            fatalError("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
        }

        return coordinator
    }()

它是這樣或withExtension:參數無關我xcdatamodeld文件擴展名,我應該使用"momd"呢? 類似的問題,我發現我指出兩個方向。 非常感謝你可以給它任何解釋。

您應該使用“momd”作為模型中的文件擴展名。 在Xcode的編譯過程中,您的.xcdatamodeld文件被編譯成一個.momd文件,這是實際被包含在捆綁。

不過, url在persistentStoreCoordinator定義變量是指NSPersistentStore文件,這對於一個sqlite的商店將具有擴展名“.sqlite”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM