swift - 表达式解析为具有特殊功能的未使用函数
问题描述
我有这个函数
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)
}
解决方案
您使用参数“at indexPath”创建了类型为“IndexPath”的方法,例如,您应该使用根据参数调用方法。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
self.deleteDayTracker(at: indexPath)
}
}
推荐阅读
- redirect - 如何使用查询字符串重定向 IIS url
- reactjs - 如何在reactjs中更改材质ui徽章内容的字体大小?
- javascript - 从外部 window.EventListener 调用函数
- python - 如何检查使用 tkinter 输入的文本是否是 int?(蟒蛇和tkinter)
- java - Java获取两个日期之间的天数
- ruby-on-rails - Rails 6.1.3 - 获取 ARGV 参数
- python - csv文件解析并使其成为dict
- amazon-web-services - 不存在的导出/输出阻止堆栈更新/删除
- c# - 从基类对受保护属性进行单元测试的正确方法
- node.js - docker build 在 npm install 上失败,原因:连接 ECONNREFUSED 104.16.17.35:443