I have a retain cycle when defining a closure as a variable.
The variable is defined as below:
public class MyCell: UICollectionViewCell {
public var callback: ((MyCell)->Void)?
}
If I use delegates instead of closures, the retain cycle disappears, but I would like to know how can it be defined with closures for future cases.
I tried to set callback variable as weak
, but, as I suppose, weak attribute can only be applied to class and class-bound protocol types.
EDIT
Usage:
class CustomController: UIViewController {
private func onActionOccurs(_ cell: MyCell) {
cell.backgroundColor = .red // For example
}
// After dequeuing the cell:
{
cell.callback = onActionOccurs(_:)
}
}
Thanks