【发布时间】:2015-10-14 11:34:04
【问题描述】:
请看以下代码:
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' 的无效转换
我知道问题是 managedObjectContext.save() 会抛出错误,这在完成处理程序中是不允许的。我发现一些博客文章修改了闭包参数以使闭包中的错误处理可行。虽然这里函数的定义是由苹果给出的,那么我该如何解决这个问题呢?非常感谢! :D
【问题讨论】:
标签: ios error-handling closures swift2