代码之家  ›  专栏  ›  技术社区  ›  Annabelle Sykes

错误:无法将类型“(_,_)->Void”的值转换为预期的参数类型“((UIAlertAction)->Void)?”

  •  0
  • Annabelle Sykes  · 技术社区  · 9 年前

    我在我的 Main.storyboard ,具有名称(a 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
    

    我在第三行得到错误(从 alertController.addAction )说:

    无法转换类型为“(_,_)->”的值;将“无效”设置为预期的参数类型“((UIAlertAction)->无效)?”

    我该如何解决这个问题?

    1 回复  |  直到 9 年前
        1
  •  3
  •   Sebastian    9 年前

    的处理程序参数 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