ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures

标签 ios swift swift3

我升级到 Swift 3.1,我收到了一些新错误,这些错误似乎是 3.1 语法问题,因为在迁移之前它们不是问题。它们大多与闭包有关,如本例所示:

let alert = UIAlertController(title: "Success", message: "Thanks for participating in our raffle!", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {

                performSegue(withIdentifier: "to_root", sender: self)

            }))

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

关于如何更正此问题以至少在短期内编译我的代码的任何想法?

谢谢。

最佳答案

您的处理程序的输入是 (UIAlertAction) 类型,因此只需将以下行添加到您的代码中。

handler: {
                action in

完整的解决方案

let alertVC = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
        alertVC.addAction(UIAlertAction(title: "Ok", style: .default, handler: {
            action in
            self.performSegue(withIdentifier: "go", sender: self)
        }))

关于ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43589259/

相关文章:

ios - 以编程方式向已以编程方式添加到表格单元格的标签添加约束

swift - SigQuit 在 Swift 3、Xcode 8.2.1 中的含义

ios - 如何将按钮添加到 iPad 弹出框内的工具栏?

ios - UIWebView 只给出一个白屏 - Xcode 7

ios - UINavigationController 转换委托(delegate)

ios - 我可以在 Swift 中使用静态下标吗?

ios - NSString正则表达式查找和替换

从 iOS 代码接收数据的 PHP 文件不起作用

arrays - 无法快速将 [Int] 转换为整数

ios - 使用委托(delegate)从 UIView 子类向 UIViewController 发送数据