属性未在super.init调用中初始化 [英] Property not initialized at super.init call

查看:472
本文介绍了属性未在super.init调用中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中有以下 init 方法:

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.
    }

self.setupScrollView 现在的方法看起来像这样:

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)
    }

我得到的错误是:

在super.init调用之前自己使用属性'self.scrollView'未在super.init调用中初始化

我看过没有运气的类似帖子。任何帮助将不胜感激!

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

推荐答案

您可以通过声明 scrollView 作为可选项,并在 super.init 调用下移动设置方法调用。声明如下:

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?

初始化:

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!)
}

这篇关于属性未在super.init调用中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆