繁体   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