Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

我在 Main.storyboard 中创建了一个按钮,它的名称(var)名为 deleteButton。这个按钮在每个单元格中。

当用户按下按钮时,会弹出一个警告以确认删除。这是代码。

@IBAction func deletePinpoint(sender: UIButton) {

    let  alertController = UIAlertController(title: "Delete pinpoint", message: "Do you want to delete this pinpoint?", preferredStyle: UIAlertControllerStyle.Alert)

    alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in

        if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
            let pinpointToDelete = self.fetchResultController.objectAtIndexPath(sender.tag) as! Pinpoints
            managedObjectContext.deleteObject(pinpointToDelete)

            do {
                try managedObjectContext.save()
            } catch {
                print(error)
            }
        }
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

    self.presentViewController(alertController, animated: true, completion: nil)
}

cellForRowAtIndexPath: 方法中,我声明了这一行:

cell.deleteButton.tag = indexPath.row

我在第 3 行(从 alertController.addAction 开始)收到错误消息:

Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

我该如何解决这个问题?

UIAlertAction 构造函数的处理程序参数,将选定的操作对象作为其唯一参数。

而不是这个:

alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in

你会想要这个:

alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction) -> Void in