I have following code from playground:
class MyButton {
var pressed: (() -> Void)?
func press() {
pressed?()
}
}
class SomeView {
var buttonDidPressed: (() -> Void)?
let button = MyButton()
init() {
button.pressed = buttonDidPressed
}
func press() {
button.press()
}
}
let view = SomeView()
view.buttonDidPressed = {
print("pressed")
}
view.press()
MyButton has closure pressed which is executed when press is called. In SomeViews init, I assign value to that pressed closure. Of course, at the init, it is nil. But after initialization of SomeView, I assign value to that buttonDidPressed closure. Because, closures are reference type, when view.press is called, buttonDidPressed should not be nil. But, anyway, my print is not executed. Why?