“在super.init初始化自身之前在方法调用中使用self",无法通过方法调用来初始化属性 [英] 'Use of self in method call before super.init initializes self', can't init properties through a method call

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

问题描述

我很好奇,无论如何在init方法内部调用一个方法来设置类的实例属性.本质上,我只是有一个类对UIView进行了子类化,并在init中添加了一些子视图,而其中一些子视图是该类的实例变量.

I'm curious is there is anyway to call a method inside your init method that sets instance properties of the class. Essentially I just have a class that sub-classes UIView, adds some subviews in init, and some of those subviews are instance variables of the class.

class MyView: UIView {
    var collectionView: UICollectionView

    convenience init () {
        self.init(frame:CGRectZero)
    }

    override init (frame : CGRect) {
        super.init(frame : frame)

        addSubviews()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        addSubviews()
    }

    func addSubviews (){
        self.collectionView = UICollectionView()

    }
}

现在出现了问题,我无法在初始化类的内部属性之前调用super init(在super.init调用中未初始化属性"self.collectionView"),但我也无法调用自定义方法来初始化那些super.init之前的变量,因为它不能在该初始化之前使用self.我意识到我可以将实例变量设置为可选变量,但是它看起来不太优雅,因为我知道它将始终被初始化(还有更多实例,这只是简化版本).有没有什么方法可以使我的所有实例变量都为可选变量呢?

Now the problem comes that I can't call super init before initializing my classes internal properties (Property 'self.collectionView' not inialized at super.init call), but I also can't call my custom method to initialize those variables prior to super.init, as it cannot use self prior to that initialization. I realize I could make the instance variable optional, but it seems less elegant, as I know it will always be initialized (and there are several more, this is just a simplified version). Is there any way to accomplish this without making all my instance variables optionals?

我认为最终我的问题是,为什么在调用super.init之前swift不允许调用方法?之间有什么区别

I think ultimately my question is why does swift dis-allow calling a method prior to calling super.init? What's the difference between:

override init (frame : CGRect) {
    addSubviews()
    super.init(frame : frame)
}

final func addSubviews (){
    self.collectionView = UICollectionView()
}

override init (frame : CGRect) {
    self.collectionView = UICollectionView()
    super.init(frame : frame)
}

推荐答案

表单文档:

Swift的编译器执行四项有用的安全检查,以确保 两阶段初始化完成,没有错误:

Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error:

安全检查1 ,指定的初始化程序必须确保所有 由其类引入的属性在委托之前已初始化 直到超类初始化程序.

Safety check 1 A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.

您需要在调用super.init()之前为实例变量设置值,并且在执行此操作之后,您将可以访问调用实例方法.您可以这样做:

You need to set value for instance variable before you call super.init() And after this action you will access to call instance methods. In your case you can do this:

override init (frame : CGRect) {
    self.collectionView = UICollectionView()
    super.init(frame : frame)
    // Now you can call method
    self.someMethod()
}

添加问题编辑的地址:

出于安全原因,不能在super.init()调用之前调用方法.如果您愿意这样做,那么您的方法可以使用一些尚未在父类中初始化的属性

You can't call method before super.init() call because of safety reasons. If you will do it then your method can use some properties which have not yet been initialized in the parent class

这篇关于“在super.init初始化自身之前在方法调用中使用self",无法通过方法调用来初始化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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