![](/img/trans.png)
[英]Swift Closures: Cannot convert return expression of type '()' to return type 'LiveSearchResponse?'
[英]Swift 3.1: Cannot convert value of type '(_) -> ()' error / Problems with closures
我升級到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)
}))
無法將'() - > Void'類型的值轉換為預期的參數類型'((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)
}))
發布導致錯誤的整個代碼塊。 聽起來好像是在嘗試為變量分配閉包而不是閉包的結果。
嘗試在閉包表達式的末尾添加(),這將導致編譯器嘗試計算閉包並使用它的返回值而不是閉包本身:
TextRow(){ row in
row.title = "First Name"
row.placeholder = "John"
row.add(rule: RuleRequired())
}()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.