简体   繁体   English

使用Swift初始化属性时遇到麻烦

[英]Having trouble initializing property with Swift

I'm using XLForm , which has a class XLFormDescriptor , which needs to be initialized with init(title: String) . 我正在使用XLForm ,该类具有XLFormDescriptor类,需要使用init(title: String)进行初始化。

The title I want to use is the return value of my current class's name function (class-level properties aren't a feature yet). 我要使用的标题是当前类的名称函数的返回值(类级别的属性尚未提供)。

Putting this at a class level, the code to set it up looks like this: 将其放在类级别,设置它的代码如下所示:

let settingsForm = XLFormDescriptor(title: self.name())

But this gives the error: 但这给出了错误:

'PanelController -> () -> PanelController!' 'PanelController->()-> PanelController!' does not have a member named 'name' 没有名为“ name”的成员

Putting this at the top of the class's init call looks like this: 将其放在类的init调用的顶部,如下所示:

let settingsForm: XLFormDescriptor

override init() {
    self.settingsForm = XLFormDescriptor(title: self.dynamicType.name())

    super.init()
}

And doing that gives this error: 这样做会产生此错误:

'self' used before super.init call 在super.init调用之前使用“自我”

Putting it after super.init() gives this error: 将其放在super.init()之后会出现此错误:

Property 'settingsForm' not initialized at super.init call super.init调用未初始化属性“ settingsForm”

Any ideas how I can possibly do this? 任何想法,我怎么可能做到这一点?

EDIT: A workaround is to do this: 编辑:一种解决方法是执行此操作:

let settingsForm = XLFormDescriptor(title: "")

override init() {
    super.init()
    self.settingsForm = XLFormDescriptor(title: self.dynamicType.name())
}

In swift, self is not available until all class properties have been initialized. 快速地,在所有类属性都已初始化之前, self不可用。 There's a check at compilation time for that. 编译时需要检查一下。

So if you have a class with properties, inherited from another class: 因此,如果您有一个具有属性的类,该类是从另一个类继承的:

class A {
    var prop1: Int
    init(val1: Int) {
        self.prop1 = val1
    }
}

class B : A {
    var prop2: String
    override int(val1: Int, val2: String) {
        // First initialize properties of this class
        self.prop2 = val2

        // Next call a superclass initializer
        super.init(val1: val1)

        // From now on, you can use 'self'
    }
}

you cannot use self until all (non optional) class properties have been been initialized and a superclass initializer has been invoked (if the class is inherited). 您必须先初始化所有 (非可选)类属性调用超类初始化程序(如果该类已继承),然后才能使用self

If you initialize a property inline, like this: 如果您初始化一个内联属性,如下所示:

let settingsForm = XLFormDescriptor(title: self.name())

you are explicitly using self before the class instance has been properly initialized - that's the reason of the compilation error. 您已经在正确初始化类实例之前显式使用了self这就是编译错误的原因。

The solution you found yourself is not a workaround, but the right way of doing it. 您发现自己的解决方案不是解决方法,而是正确的解决方法。 You first initialize the property with a value not referencing self , then once the class has been initialized, you assign that property a new value. 首先,使用不引用self的值初始化属性,然后在初始化类后,为该属性分配新值。

Note that this is the only case where you are allowed to assign a new value to an immutable property, as long as it is done from within an initializer. 请注意,这是唯一允许您将新值分配给不可变属性的情况,只要它是在初始化程序中完成的。

For a better understanding of how initialization works, I recommend reading the corresponding documentation: Initialization 为了更好地了解初始化的工作方式,建议阅读相应的文档: 初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM