'self' usage inside constant property

Hello, can somebody help to understand if following is not handled case for compiler or it is inside UIKit?

I have next code inside View Controller subclass:

private let gestureRecognizer: UITapGestureRecognizer = {
    let tap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.handleTap(_:)))
    return tap
}()

UIGestureRecognizer.init expects Any? type for target param.

This code compiles successfully, but gesture recogniser doesn't work.
And I see that self printed in console as (Function) and when I changed property type to be lazy var it printed correct address of object. As I remember we can't use self in constant properties before the class is fully initialised.

Your question is answered on stack overflow here:

2 Likes
Terms of Service

Privacy Policy

Cookie Policy