Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can i use self when I initialize property with a closure?

Tags:

ios

swift

Official docs (Swift 4.1) says:

If you use a closure to initialize a property, remember that the rest of the instance has not yet been initialized at the point that the closure is executed. This means that you cannot access any other property values from within your closure, even if those properties have default values. You also cannot use the implicit self property, or call any of the instance’s methods.

So I guess i need to use lazy var,today I created a button

let loginRegisterButton: UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor.rgb(red: 80, green: 101, blue: 161)
    button.setTitle("Register", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitleColor(UIColor.white, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
    button.addTarget(self , action: #selector(handleRegister), for: .touchUpInside)
    return button
}()

so I just used self in addTarget method and XCode did not seem to bother even more program went without any errors. Everything worked out well.So Why can i use self when I initialize property with a closure? Is there some changes or maybe I missed something?

like image 287
Ninja Avatar asked Jan 28 '23 02:01

Ninja


1 Answers

The target in your code is probably nil, the reason that this might work is that:

...If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object

(from the documentation of the method)

You can verify that the target is actually nil by setting a breakpoint during or after your controller's init and by inspecting the _targetActions array (in the debugger's variables view) of the button (you can see that target's address is 0x0).

In your example, loginRegisterButton is set during the controller's initialization which means that there is no self yet (in other words you cannot guarantee that all instance variables are initialized at this point). The way lazy solves this is that the actual assignment is deferred on first access.

like image 162
Alladinian Avatar answered Feb 13 '23 05:02

Alladinian