首页 > 解决方案 > 表达式解析为具有特殊功能的未使用函数

问题描述

我有这个函数 func deleteDayTracker(at indexPath: IndexPath),我想在 alertAction 中调用它,所以当按下警报操作按钮时,会调用 deleteDayTracker 函数和其中的代码。我试过ViewController.deleteDayTracker(self)但后来我得到错误表达式解析为一个未使用的函数。如何在 alertAction 中调用 deleteDayTracker。这是你的完整代码

func deleteDayTracker(at indexPath: IndexPath)
{
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    context.delete(numberOfDays[indexPath.row])
    context.delete(eventName[indexPath.row])
    context.delete(eventDate[indexPath.row])


    do
    {
        try context.save()
    }
    catch
    {
        let alert = UIAlertController(title: nil, message: "Unable to delete event", preferredStyle: .alert)
        let okButton = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(okButton)
        self.present(alert, animated: true, completion: nil)
    }
    tableView.reloadData()
}


@IBAction func editButton(_ sender: Any)
{

    let editAlert = UIAlertController(title: nil, message: "Delete this day tracker?", preferredStyle: .actionSheet)
    let deleteButton = UIAlertAction(title: "Delete", style: .destructive, handler: self.deleteCell)
    let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    editAlert.addAction(deleteButton)
    editAlert.addAction(cancelButton)
    if let alert = editAlert.popoverPresentationController
    {
        alert.sourceView = self.view
        alert.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
        alert.permittedArrowDirections = []
    }

    self.present(editAlert, animated: true, completion: nil)
}

func deleteCell(alert: UIAlertAction!)
{
    ViewController.deleteDayTracker(self)
}

标签: swiftfunction

解决方案


您使用参数“at indexPath”创建了类型为“IndexPath”的方法,例如,您应该使用根据参数调用方法。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

     UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
         self.deleteDayTracker(at: indexPath)
   }
}

推荐阅读