如何在swift中关闭错误? [英] how to throw errors in a closure in swift?

查看:129
本文介绍了如何在swift中关闭错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

   let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {
        (action : UITableViewRowAction, indexPath : NSIndexPath)  -> Void  in

        if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext{
            let restaurantToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Restaurant
            managedObjectContext.deleteObject(restaurantToDelete)

            // Saving managedObjectContext instance, and catch errors if it fails
            do {
                try managedObjectContext.save()
            } catch let error as NSError {
                print("Error: \(error.localizedDescription)")
            }

        }

    })
    return deleteAction
}

Xcode的错误消息是:无效的转换(UITableViewRowAction,NSIndexPath)throws - > Void'抛出非抛出函数类型'(UITableViewRowAction,NSIndexPath) - > Void'

the error message from Xcode is : Invalid conversion from throwing function of type '(UITableViewRowAction, NSIndexPath) throws -> Void' to non-throwing function type '(UITableViewRowAction, NSIndexPath) -> Void'

我知道问题是managedObjectContext.save()将抛出错误,这在完成处理程序中是不允许的。我发现一些博客文章,他们修改了闭包参数,以使封装中的错误处理可行。虽然这里功能的定义是由苹果提供的,那么我该如何解决这个问题呢?非常感谢! :D

I know the problem is managedObjectContext.save() will throw errors and this is not allowed in the completion handler. I found some blog articles where they modified the closure parameters in order to make the error handling in a closure workable. While here the definition of the function is given by apple, so how can i fix this issue? Thanks a lot! :D

推荐答案

编译器将 throws 添加到您的因为您的 catch 子句并不详尽:模式匹配让错误为NSError 可能会失败...请参阅< a href =https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID512 =noreferrer >文档

the compiler is adding throws to the signature of your block because your catch clause is not exhaustive: the pattern match let error as NSError can fail... see the documentation

闭包参数的签名是(UITableViewRowAction,NSIndexPath) - > Void ,但编译器推断您提供的关闭类型为(UITableViewRowAction,NSIndexPath)throws - >通过在你之后添加另一个 catch 子句(没有模式),无效

the signature of the closure argument is (UITableViewRowAction, NSIndexPath) -> Void, however the compiler is inferring the type of the closure that you are providing to be (UITableViewRowAction, NSIndexPath) throws -> Void

已经有编译器会看到你在本地捕获异常,不再推断你提供的关闭的签名包括 throws

by adding another catch clause (with no pattern) after the one you already have the compiler will see that you are catching the exception locally and it will no longer infer that the signature of the closure you are providing includes throws:

do {
  try managedObjectContext.save()
} catch let error as NSError {
  print("Error: \(error.localizedDescription)")
} catch {}

这篇关于如何在swift中关闭错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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