I understand the typical retain cycle with a closure when referencing self within the closure, but is there a reason the label does not produce a retain cycle? I've tested commenting the testClosure out vs commenting the label out and only the testClosure produced a retain cycle
class TargetView: UIView {
let newText = "hi"
var label = UILabel()
private var counter = 0
private var testClosure : (() -> ()) = { }
init() {
super.init(frame: .zero)
// This causes a retain cycle
testClosure = {
self.counter += 1
print(self.counter)
}
// This does NOT cause a retain cycle
label = UILabel() {
$0.text = self.newText
self.counter += 1
}
}
required init?(coder aDecoder: NSCoder) { fatalError() }
deinit {
print("view deinit!")
}
}
extension UILabel {
convenience init(label: (UILabel) -> Void) {
self.init()
label(self)
}
}