简体   繁体   English

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

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

First of all, thank you so much for helping me.首先,非常感谢您对我的帮助。 I do not understand this error and am unable to find a solution.我不明白这个错误,也找不到解决办法。

I have looked at other answers but they didn't seem to help.我看过其他答案,但它们似乎没有帮助。 Here is my code.这是我的代码。 The error shows up on the let action line.错误显示在 let 操作行上。

  @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {

    var textField = UITextField()

    let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)


    let action = UIAlertAction(title: "Add item", style: .default) { (UIAlertAction) in





        //what will happen once the user clicks the Add Item button on our UIAlert

        let newItem = item()
        newItem.title = textField.text!
        self.itemArray.append(newItem)

   let encoder = PropertyListEncoder()

        do {

        let data = try encoder.encode(self.itemArray)
        }

        catch {

            try Data.write(to: self.dataFilePath!)
            print("Error encoding item array, \(error)")

        }


        //reloads table view data
        self.tableView.reloadData()

      }

alert.addTextField { (alertTextField) in

        alertTextField.placeholder = "What Needs To Be Done?"
      textField = alertTextField
    }

alert.addAction(action)
  present(alert, animated: true, completion: nil)

} }

Error: Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?'错误:无法将类型“(_) throws -> ()”的值转换为预期的参数类型“((UIAlertAction) -> Void)?”

Three issues:三个问题:

  1. The parameter in the action must be an instance, not a type动作中的参数必须是实例,而不是类型

    let action = UIAlertAction(title: "Add item", style: .default) { action in

    or if the action is unused或者如果操作未使用

    let action = UIAlertAction(title: "Add item", style: .default) { _ in
  2. Data.write(to:.. must be data.write(to:.. Data.write(to:..必须是data.write(to:..

  3. The third issue causes the (a bit misleading) error message: the data line must be inside the do block.第三个问题导致(有点误导)错误消息: data行必须在do块内。 The error indicates that there is no do block around the try statement错误表明try语句周围没有do

    do { let data = try encoder.encode(self.itemArray) try data.write(to: self.dataFilePath!) } catch { print("Error encoding item array, \\(error)") }

暂无
暂无

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

相关问题 UIAlert无法将类型&#39;(_)throws-&gt;()&#39;的值转换为预期的参数类型&#39;(((UIAlertAction)-&gt; Void)? - UIAlert 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