![](/img/trans.png)
[英]Cannot convert value of type '(_) throws -> ()' to expected argument type '((UIAlertAction) -> Void)?'
[英]Error: Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'
我有我創建了一個按鈕Main.storyboard
,其中有一個名稱( var
命名) deleteButton
。 該按鈕在每個單元格中。
當用戶按下按鈕時,彈出警報以確認刪除。 這是代碼。
@IBAction func deletePinpoint(sender: UIButton) {
let alertController = UIAlertController(title: "Delete pinpoint", message: "Do you want to delete this pinpoint?", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
let pinpointToDelete = self.fetchResultController.objectAtIndexPath(sender.tag) as! Pinpoints
managedObjectContext.deleteObject(pinpointToDelete)
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
在cellForRowAtIndexPath:
方法中,我聲明了這一行:
cell.deleteButton.tag = indexPath.row
我在第三行收到錯誤(以alertController.addAction
):
無法將類型'(_,_)-> Void'的值轉換為預期的參數類型'((UIAlertAction)-> Void)?
我該如何解決?
UIAlertAction
構造函數的處理程序參數將選定的操作對象作為其唯一參數。
代替這個:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action, indexPath) -> Void in
您將需要:
alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction) -> Void in
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.