简体   繁体   English

UIAlert无法将类型'(_)throws->()'的值转换为预期的参数类型'(((UIAlertAction)-> Void)?

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

I've been looking for solutions to an issue I'm having with some code in a UIAlertAction . 我一直在寻找解决UIAlertAction某些代码的问题的解决方案。 I want to update a value in an SQLite database from true to false for a given number. 我想将给定数字的SQLite数据库中的值从true更新为false。 I've managed to get the if/else loops working so I know I'm getting into the right place. 我设法使if / else循环正常工作,所以我知道我来对地方了。 But when I try and run the database update query from the UIAlertAction, I get the above error. 但是,当我尝试从UIAlertAction运行数据库更新查询时,出现上述错误。

Code below: 代码如下:

} else {
    print ("Badge in School - check out")
    let checkOut = UIAlertController(title: "Sign Out?", message: "Click on the 'Sign Out' button below to end your visit", preferredStyle: .alert)

    let signOut = UIAlertAction(title: "Sign Out", style: .destructive) { (UIAlertAction) in
        print ("Set Badge State to false")

        let badgeOut = self.badgeTable.filter(self.badgeNumber == badgeNumberInt!)
        let badgeOutUpdate = badgeOut.update(self.badgeIn <- false)

        try self.myData.run(badgeOutUpdate)
    }

    let cancel = UIAlertAction(title: "Cancel", style: .cancel)

    checkOut.addAction(signOut)
    checkOut.addAction(cancel)

    present (checkOut, animated: true, completion: nil)
}

If I change: 如果我更改:

try self.myData.run(badgeOutUpdate)

to

try! self.myData.run(badgeOutUpdate)

the code runs and does exactly what I'd expect. 该代码将运行,并且完全符合我的期望。

Any explanations or pointers to documents that can explain it to an idiot like me would be gratefully received! 我们将不胜感激地收到任何可以向像我这样的白痴解释的文件说明或指针!

The run method you are calling can throw an exception, but the alert action closure does not expect an exception to be thrown. 您正在调用的run方法可能会引发异常,但是警报操作关闭不会引发异常。 You must handle the exception because the alert action is not setup to allow the exception to bubble up. 您必须处理异常,因为未设置警报操作以允许异常冒泡。 When you add the ! 当您添加! to the try, you're saying "do this and if an exception happens, crash the app." 尝试时,您说的是“执行此操作,如果发生异常,则使应用程序崩溃。” Another alternative is to wrap it in a do-catch block: 另一种选择是将其包装在do-catch块中:

do {
    try self.myData.run(badgeOutUpdate)
} catch {
    // Handle the exception here
}

If an exception is encountered, it will execute the catch block. 如果遇到异常,它将执行catch块。 If not, the catch will be ignored. 如果不是这样,则捕获将被忽略。 Another option is to use the following: 另一种选择是使用以下内容:

try? self.myData.run(badgeOutUpdate)

This will perform the call that can throw. 这将执行可能引发的呼叫。 If that call does throw an exception, it fails silently and your program continues on. 如果该调用确实引发了异常,则它会静默失败并继续执行您的程序。 This may be fine in some instances, or it may mean that nothing else will work in your program. 在某些情况下这可能很好,或者可能意味着您的程序中没有其他可用的方法。

Each situation is different, choose which works best for you: crash on exception, handle the exception, fail silently. 每种情况都不同,请选择最适合您的方法:发生异常崩溃,处理异常,静默失败。

The Swift documentation covers this pretty well: https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html Swift文档很好地涵盖了这一点: https : //docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html

You need do-catch block 您需要do-catch

do {
      try self.myData.run(badgeOutUpdate)
  }
catch {
       print(error)
}

暂无
暂无

声明:本站的技术帖子网页,遵循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)?' 错误:无法将类型&#39;(_,_)-&gt; Void&#39;的值转换为预期的参数类型&#39;(((UIAlertAction)-&gt; Void)? - Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?' 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> 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' 无法将“Void”类型的值转换为预期的参数类型“(String) -> Void” - Cannot convert value of type 'Void' to expected argument type '(String) -> Void' 无法将&#39;Int&#39;类型的值转换为预期的参数类型&#39;(inout UnsafeMutableBufferPointer &lt;_&gt;,inout Int)throws - &gt; Void&#39; - Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void' 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;(()-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?' 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;((Bool,Error?)-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?' 无法转换类型&#39;(_)-&gt;()的值? 到预期的参数类型&#39;@convention(block)()-&gt; Void&#39; - Cannot convert value of type '(_) -> ()?' to expected argument type '@convention(block) () -> Void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM