调用中的Swift 2额外参数'错误' [英] Swift 2 Extra argument ' error' in call

查看:84
本文介绍了调用中的Swift 2额外参数'错误'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 7将项目从Swift更新到Swift2,并且出现了CoreData错误:

Hi i'm updating my project from Swift to Swift2 with Xcode 7 and i'm getting this CoreData error :

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
    }()

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

now i get error in first line how can i fix that ?? Thanks

推荐答案

Swift 2现在提供了try/catch机制,可可API已被重写为使用此机制,而不是返回传统的Objective C方式的错误.

Swift 2 now provides a try/catch mechanism, and Cocoa APIs have been rewritten to use this instead rather than returning the error the traditional Objective C way.

您现在应该这样做:

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

} 

这篇关于调用中的Swift 2额外参数'错误'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆