In trying to avoid retain cycles, would using [weak self] in
in a UITableViewCell button action be necessary? Example:
in ViewController's cellForRow
cell.buttonAction = { (cell) [weak self] in
self.someFunction()
}
in TableViewCell class
var buttonAction: ((UITableViewCell) -> Void)?
@IBAction func buttonPressed(_ sender: Any) {
buttonAction?(self)
}
Yes, it is necessary to use
unowned
orweak
to captureself
in this case.UITableView
UITableViewCells
andbuttonAction
closure.Using self directly will have as an effect a retain cycle.
This is actually pretty easy to test. Try to present the following view controller and dismiss it:
You will have the following output:
Now presenting the same view controller without weak and you will see that there is no output, meaning that the
deinit
functions are not get called and the objects stay in the memory.