我刚在Core Data上建立了一个只读数据存储。它有效,但编译器抱怨它,我想知道如何解决这个问题。
这里是我在Xcode自动生成模板后修改它的相关代码。
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "MyApp")
let appName: String = "MyApp",
storeUrl = Bundle.main.url(forResource: appName, withExtension: "sqlite")
var persistentStoreDescriptions: NSPersistentStoreDescription
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl
container.persistentStoreDescriptions = [description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
现在这是我从编译器得到的消息:
[error] CoreData: error: Attempt to add read-only file at path ...../MyApp.app/MyApp.sqlite read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
如何设置此选项以便编译器满意?
答案 0 :(得分:3)
在说明
中设置选项description.setOption(NSNumber(value: true), forKey: NSReadOnlyPersistentStoreOption)
顺便说一句:变量persistentStoreDescriptions
未使用。