[英]What memory management is appropriate for a UITableViewRowAction closure?
在以下情況下,我對自己使用unown以及對tableView既不弱也不對unown合適嗎?
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]
}
在這種情況下,我認為您不需要任何capture list
。
使用捕獲列表的時間是我們創建一個強大的參考周期時,這意味着這些對象相互指向,並且由於計數不為0
,因此ARC將認為它們仍在使用中。
在editActionsForRowAt
情況下,閉包不指向其他任何內容,而只是要執行的代碼塊。
輕觸操作按鈕之一將執行與操作對象一起存儲的處理程序塊。
在此處閱讀有關editActionsForRowAt
更多信息
總之,刪除[unowned self]
是安全的,並且因為您沒有使用action
,所以可以將其替換為_
以使其更加整潔。 而且您也不必在這里調用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如何工作以及何時使用捕獲列表的出色示例。 您也可以簽出。 鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.