简体   繁体   English

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

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

I have a button created in my Main.storyboard , which has a name (a var ) named deleteButton . 我有我创建了一个按钮Main.storyboard ,其中有一个名称( var命名) deleteButton This button is in each cell. 该按钮在每个单元格中。

When the user presses the button, an alert pops up to confirm the deletion. 当用户按下按钮时,弹出警报以确认删除。 Here is the code. 这是代码。

@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)
}

In the cellForRowAtIndexPath: method, I have declared this line: cellForRowAtIndexPath:方法中,我声明了这一行:

cell.deleteButton.tag = indexPath.row

I get the error on the 3rd line (starting with alertController.addAction ) saying: 我在第三行收到错误(以alertController.addAction ):

Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?' 无法将类型'(_,_)-> Void'的值转换为预期的参数类型'((UIAlertAction)-> Void)?

How do I fix this? 我该如何解决?

The handler's parameter of UIAlertAction constructor, takes the selected action object as its only parameter. UIAlertAction构造函数的处理程序参数将选定的操作对象作为其唯一参数。

Instead of this: 代替这个:

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

you will want this: 您将需要:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型 &#39;(_) throws -&gt; ()&#39; 的值转换为预期的参数类型 &#39;((UIAlertAction) -&gt; Void)?&#39; - Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?' UIAlert无法将类型&#39;(_)throws-&gt;()&#39;的值转换为预期的参数类型&#39;(((UIAlertAction)-&gt; Void)? - UIAlert Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?' 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;((Bool,Error?)-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?' 无法将类型&#39;() - &gt; Void&#39;的值转换为预期的参数类型&#39;(( - - &gt; Void)?&#39; - Cannot convert value of type '() -> Void' to expected argument type '(() -> Void)?' 无法将类型“(Void)-&gt;()”的值转换为预期的参数类型“()-&gt; Void” - Cannot convert value of type '(Void) -> ()' to expected argument type '() -> Void' 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> void' 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;(()-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?' 无法转换类型&#39;(_)-&gt;()的值? 到预期的参数类型&#39;@convention(block)()-&gt; Void&#39; - Cannot convert value of type '(_) -> ()?' to expected argument type '@convention(block) () -> Void' 无法使用完成处理程序将类型“()”的值转换为预期的参数类型“()-&gt; Void” - Cannot convert value of type '()' to expected argument type '() -> Void' with a completionHandler 更新PromiseKit后发生错误:无法将类型&#39;PMKFinalizer&#39;的值转换为预期的参数类型&#39;Promise <Void> “ - Error after update PromiseKit: Cannot convert value of type 'PMKFinalizer' to expected argument type 'Promise<Void>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM