Home > Enterprise >  'weak' may only be applied to class and class-bound protocol types, not '(ViewControl
'weak' may only be applied to class and class-bound protocol types, not '(ViewControl

Time:09-27

import UIKit

class ViewController: UIViewController {

    var myAction: ((Int, Bool) -> Void)? = { [weak self] int, bool in

    }
}

I have no idea why I'm getting this error. I thought self should be an instance of ViewController, how is it becoming '(ViewController) -> () -> ViewController'?

CodePudding user response:

At the point when your property initializer runs, self is not yet available. You'll need to use lazy loading — which is pretty easy. Just add lazy in front of the var.

lazy var myAction: ((Int, Bool) -> Void)? = { [weak self] int, bool in
    
}
  • Related