Core data 无法为类型“调用初始化器”;“N错误”;使用AppDelegate coredata中的(域、代码、用户信息),Swift 2
我也刚刚更新到Xcode 7,我在AppDelegate.swift中遇到了这个问题Core data 无法为类型“调用初始化器”;“N错误”;使用AppDelegate coredata中的(域、代码、用户信息),Swift 2,core-data,swift2,appdelegate,nserror,Core Data,Swift2,Appdelegate,Nserror,我也刚刚更新到Xcode 7,我在AppDelegate.swift中遇到了这个问题 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the sto
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Inclinometer.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
} catch var error1 as NSError {
error = error1
// Report any error we got.
let dict = NSMutableDictionary()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
} catch {
fatalError()
}
return coordinator
}()
它显示在这行
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
无法使用类型为(域、代码、用户信息)的参数列表调用类型为“NSError”的初始化器。如何更改它以使其在Xcode 7中运行?初始化器要求
userInfo
的参数类型为[String:AnyObject]
。你必须这样写:
let dict : [String: AnyObject] = [
NSLocalizedDescriptionKey: "Failed to initialize the application's saved data",
NSLocalizedFailureReasonErrorKey : failureReason
NSUnderlyingErrorKey : error
]
另外,我认为您不需要创建额外的错误变量(在何处定义?)并覆盖它。似乎您必须特别明确地在XCode 7.0中放入字典中的内容,这对我很有用
let dict : [String: AnyObject] = [
NSLocalizedDescriptionKey: "Failed to initialize the application’s saved data",
NSLocalizedFailureReasonErrorKey: String(failureReason),
NSUnderlyingErrorKey: String(error),
]
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
您好,现在通过这个“let dict:”,它显示了“表达式的类型在没有更多上下文的情况下是不明确的”。你的意思是我不需要定义“error=NSError(domain:“YOUR_error_domain”,code:9999,userInfo:dict)”?此外,我认为它不仅仅需要字符串。请参阅“NSError(域:,代码:,用户信息:)”在Xcode 7.0中不起作用(问题是关于这个问题的)。“I get”类型的表达式在没有更多上下文的情况下是不明确的