Property not initialized at super.init call

 ̄綄美尐妖づ 提交于 2019-12-23 05:22:52

问题


I have the following init method in my view controller:

init(mainViewController: UIViewController, settingsViewController: UIViewController, gap: Int) {
        self.mainViewController = mainViewController
        self.settingsViewController = settingsViewController
        self.gap = gap

        self.setupScrollView() // I get error here

        super.init(nibName: nil, bundle: nil) //and here.
    }

The self.setupScrollView method for now just looks like this:

func setupScrollView() {
        self.scrollView = UIScrollView(frame: CGRectZero)
        self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(self.scrollView)
    }

The errors I get are:

self used before super.init call and Property 'self.scrollView' not initialized at super.init call

I've looked at similar post without any luck. Any help would be appreciated!


回答1:


You can do it by declaring scrollView as optional and moving the setup method call below the super.init call. The declaration would look like this:

var scrollView : UIScrollView?

And the initialisation:

init(mainViewController: UIViewController, settingsViewController: UIViewController, gap: Int) {
    //other stuff
    super.init(nibName: nil, bundle: nil)
    self.setupScrollView()
}

func setupScrollView() {
    scrollView = UIScrollView(frame: CGRectZero)
    scrollView!.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addSubview(scrollView!)
}


来源:https://stackoverflow.com/questions/24351211/property-not-initialized-at-super-init-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!