Swift 什么内存管理适合UITableViewRowAction关闭?
我对unowned for self和unowned for tableView的使用在以下上下文中是否合适Swift 什么内存管理适合UITableViewRowAction关闭?,swift,memory-management,closures,Swift,Memory Management,Closures,我对unowned for self和unowned for tableView的使用在以下上下文中是否合适 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .destructive, title: "Delete") { [
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { [unowned self] (action, indexPath) in
self.habitsManager.remove(at: indexPath.row)
self.adjustForRowCount()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
return [delete]
}
在这种情况下,我认为您不需要任何
捕获列表
使用捕获列表的时间是当我们创建一个强引用周期时,这意味着这些对象指向对方,ARC会考虑它们仍然在使用,因为计数不是<代码> 0 /代码>。p>
在
editActionsForRowAt
情况下,闭包不指向任何其他内容,只指向要执行的代码块
点击其中一个动作按钮执行与动作对象一起存储的处理程序块
阅读更多关于editActionsForRowAt
总之,删除[无主自我]
是安全的,因为您没有使用操作
,所以可以用\uuu
替换它,使其更干净。您也不必在此处调用tableView.reloadData()
let delete = UITableViewRowAction(style: .destructive, title: "Delete") {(_, indexPath) in
self.habitsManager.remove(at: indexPath.row)
self.adjustForRowCount()
tableView.deleteRows(at: [indexPath], with: .fade)
}
顺便说一下,Swift文档中有一些关于ARC如何工作以及何时使用捕获列表的好例子。你也可以去看看