![](/img/trans.png)
[英]UIAlert Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?'
[英]Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?'
首先,非常感謝您對我的幫助。 我不明白這個錯誤,也找不到解決辦法。
我看過其他答案,但它們似乎沒有幫助。 這是我的代碼。 錯誤顯示在 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)
}
錯誤:無法將類型“(_) throws -> ()”的值轉換為預期的參數類型“((UIAlertAction) -> Void)?”
三個問題:
動作中的參數必須是實例,而不是類型
let action = UIAlertAction(title: "Add item", style: .default) { action in
或者如果操作未使用
let action = UIAlertAction(title: "Add item", style: .default) { _ in
Data.write(to:..
必須是data.write(to:..
第三個問題導致(有點誤導)錯誤消息: data
行必須在do
塊內。 錯誤表明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.