请看下面的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
来自Xcode的错误消息是:从类型'(UITableViewRowAction,NSIndexPath)抛出函数的无效转换抛出 - > Void'到非投掷函数类型'(UITableViewRowAction,NSIndexPath) - > Void'
我知道问题是managedObjectContext.save()将抛出错误,这在完成处理程序中是不允许的.我发现了一些博客文章,他们修改了闭包参数,以便在闭包中使错误处理可行.虽然这里函数的定义是由apple给出的,那么我该如何解决这个问题呢?非常感谢!:d