Cannot capture weak self inside lazy var property

I have a class and a lazy var inside, like this:

class C {
  lazy var btn = {
    let btn = UIButton()
    btn.addBlock { [weak self] self?.xxx() }
    return btn
  }
}

However, the compiler said: "Cannot find 'weak' in scope"

If I change to this, it works:

class C {
  lazy var btn = {
    let btn = UIButton()
    weak var ws = self
    btn.addBlock { ws?.xxx() }
    return btn
  }
}

So, can't we use "[weak xxx]" to weak something inside a block inside a lazy var property statement?

You're missing in, so should be btn.addBlock { [weak self] in self?.xxx() }

2 Likes

Ohhhh I'm so stupid..... Thanks!!

1 Like

Np, always make this mistake also :upside_down_face:

1 Like