无法在 AppDelegate coredata,Swift 2 中使用(域,代码,userInfo)调用类型“NSError”的初始化程序

Posted

技术标签:

【中文标题】无法在 AppDelegate coredata,Swift 2 中使用(域,代码,userInfo)调用类型“NSError”的初始化程序【英文标题】:cannot invoke initialiser for type "NSError" with (domain, code, userInfo) in AppDelegate coredata, Swift 2 【发布时间】:2015-09-29 06:59:13 【问题描述】:

我也刚刚更新到 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 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 中运行?

【问题讨论】:

【参考方案1】:

NSError 初始化程序需要 [String: AnyObject] 类型的参数用于 userInfo。你必须这样写:

let dict : [String: AnyObject] = [
  NSLocalizedDescriptionKey: "Failed to initialize the application's saved data",
  NSLocalizedFailureReasonErrorKey : failureReason
  NSUnderlyingErrorKey : error
]

另外,我认为您不需要创建额外的错误变量(定义在哪里?)并覆盖它。

【讨论】:

您好,现在仅通过此“let dict:”显示“表达式类型不明确,没有更多上下文”。你的意思是我不需要定义"error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)"? 此外,我认为它不仅期望字符串。参见“NSError(domain: , code: , userInfo: ))” 在 Xcode 7.0 中不起作用(问题询问)。我得到“没有更多上下文的表达式类型不明确【参考方案2】:

似乎您必须更加具体地说明您在字典中输入的内容,在 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)

【讨论】:

以上是关于无法在 AppDelegate coredata,Swift 2 中使用(域,代码,userInfo)调用类型“NSError”的初始化程序的主要内容,如果未能解决你的问题,请参考以下文章

用 CoreData 对象填充 NSSet

无法在 CoreData swift 中保存数据

如何在 OSX 应用程序中将 CoreData 管理与 AppDelegate 分离

如何解决 AppDelegate 中的 coreData 错误?

Swift 3.0 Coredata - Appdelegate & managedObjectContext 抛出 EXC_BAD_ACCESS 错误

无法获取 managedObjectContext 对象