简体   繁体   English

限定Bundle.main.url使用核心数据为iOS9和上述夫特4

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

I'm just learning Core Data and I need to implement Core Data for both iOS 9 and iOS 10 as my only iPad test device is an iPad3 running iOS 9.3.5 .我只是学习核心数据,我需要实现Core Data两者iOS 9iOS 10作为我唯一的iPad测试设备是运行的iPad3 iOS 9.3.5 I'm trying to follow this solution https://charleswilson.blog/2016/09/09/out-of-context-using-core-data-on-ios-9-3-and-10-0/ ( not sure I could paste the whole code from the link ) as I failed with implementing other solutions from stack overflow.我试图按照这种解决方案https://charleswilson.blog/2016/09/09/out-of-context-using-core-data-on-ios-9-3-and-10-0/ (不相信我可以粘贴链接整个代码),因为我未能从堆栈溢出实施其他解决方案。 I'm not sure If I got this one thing right: Inside the lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator there is this let modelURL = Bundle.main.url(forResource: modelName, withExtension: modelExtension)!我不知道如果我得到了这样一件事右:里面的lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator还有这let modelURL = Bundle.main.url(forResource: modelName, withExtension: modelExtension)! that I see in other solutions here in stack overflow and they're all declared as with different String values for forResource parameter, but all with the same "momd" value for withExtension: parameter.我在其他的解决方案,在这里看到的堆栈溢出和他们都声明为不同的String的值forResource参数,但都具有相同的"momd"withExtension:参数。 I actually thought the since I'm using an .xcdatamodeld I should put in my data model name for forResource parameter and "xcdatamodeld" for withExtension: parameter, resulting in my case as :其实,我认为,因为我使用的是.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
    }()

Is it so or withExtension: parameter is unrelated to my xcdatamodeld file extension and I should use "momd" instead?它是这样或withExtension:参数无关我xcdatamodeld文件扩展名,我应该使用"momd"呢? Similar questions I found point me in both directions.类似的问题,我发现我指出两个方向。 Many thanks for any explanation you could give about it.非常感谢你可以给它任何解释。

You should use “momd” as the file extension for the model.您应该使用“momd”作为模型中的文件扩展名。 During the Xcode compilation process, your .xcdatamodeld file gets compiled into a .momd file, which is what actually gets included in the bundle.在Xcode的编译过程中,您的.xcdatamodeld文件被编译成一个.momd文件,这是实际被包含在捆绑。

However, the url variable in the persistentStoreCoordinator definition refers to the NSPersistentStore file, which for a sqlite store will have the extension “.sqlite”.不过, url在persistentStoreCoordinator定义变量是指NSPersistentStore文件,这对于一个sqlite的商店将具有扩展名“.sqlite”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM