gpt4 book ai didi

ios - Swift 2 调用中的额外参数 ' error'

转载 作者:可可西里 更新时间:2023-11-01 03:05:55 24 4
gpt4 key购买 nike

您好,我正在使用 Xcode 7 将我的项目从 Swift 更新到 Swift2,我收到了这个 CoreData 错误:

Extra argument 'error' in call

在这一行

if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {

编辑

这是我的代码:

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("Xipe_Tech.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."

do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
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 let error as NSError {
print(error.localizedDescription)

}

return coordinator
}()

现在我在第一行得到错误我该如何解决??谢谢

最佳答案

Swift 2 现在提供了一个 try/catch 机制,并且 Cocoa API 已经被重写以使用它而不是传统的 Objective C 方式返回错误。

你现在应该这样做:

do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch let error as NSError {
print(error.localizedDescription)

}

关于ios - Swift 2 调用中的额外参数 ' error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31113479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com