为什么在我用Swift初始化变量的行上出现“在初始化之前使用的变量”错误?

时间:2022-08-22 22:44:35

I'm struggling to understand why I'm getting this compiler error in an iOS project using Swift. If I create the following class:

我很难理解为什么在使用Swift的iOS项目中会出现编译错误。如果我创建了以下类:

class InitTest {

    let a: Int
    let b: Int
    let c: Int

    init () {
        self.a = 3
        self.b = 4
        self.c = self.runCalculation()
    }

    func runCalculation () -> Int {
        return self.a * self.b
    }
}

I get a compiler error on the line self.c = self.runCalculation() saying "Variable 'self.c' used before being initialized".

我在代码行上得到一个编译错误。c = self。runcomputing(),表示“变量自我”。在初始化之前使用c。

At first I thought this was because the compiler could not verify that the runCalculation() method did not access self.c, but then I tried mixing the init method up a bit:

起初我认为这是因为编译器无法验证runCalculation()方法没有访问self。c,然后我尝试混合init方法:

init () {
    self.a = 3
    self.c = self.runCalculation()
    self.b = 4
}

and this time the error is "Variable 'self.b' used before being initialized" (on the same self.runCalculation() line). This indicates that the compiler is capable of checking which properties the method accesses, and so as far as I can see should have no issue with the initial case.

这次的错误是“可变自我”。b'在初始化之前使用”(在相同的self.runCalculation()行上)。这表明编译器能够检查方法访问的属性,因此,就我所能看到的来说,对于初始的情况应该没有问题。

Of course this is a trivial example and I could easily refactor to avoid calling the calculation method, but in a real project there could be several calculations each of which could be quite involved. I'd like to be able to separate out the logic to keep things readable.

当然,这是一个微不足道的例子,我可以很容易地重构以避免调用计算方法,但是在实际的项目中,可能会有多个计算,每个计算都可能非常复杂。我希望能够将逻辑分离出来,使其保持可读性。

Fortunately there's a simple workaround:

幸运的是,有一个简单的解决办法:

init () {
    self.a = 3
    self.b = 4

    self.c = 0
    self.c = self.runCalculation()
}

(or using a property initialiser let c = 0) but I'd like to understand why the compiler has a problem with the first example. Am I missing something or is it an unnecessary restriction?

(或者使用属性初始化器让c = 0)但是我想知道为什么编译器对第一个例子有问题。是我漏掉了什么,还是没有必要的限制?

1 个解决方案

#1


44  

Swift has this behaviour because of two phase initialisation. From Apple's Swift book:

Swift具有这种行为是因为两阶段初始化。迅速从苹果的书:

Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

Swift中的类初始化是一个两阶段的过程。在第一个阶段中,每个存储属性由引入它的类分配一个初始值。一旦确定了每个存储属性的初始状态,第二个阶段就开始了,每个类都有机会在考虑使用新实例之前进一步定制其存储属性。

Class need some kind of default value before first phase ends. Customising values is part of second phase.

类在第一阶段结束之前需要某种默认值。定制值是第二阶段的一部分。

Objective-C didn't had this behaviour, because it could always give 0 as default for primitives and nil for objects, but in Swift there is no mechanism to give such default value.

Objective-C没有这种行为,因为对于原语,它总是可以给0作为默认值,对于对象,它可以给nil,但是在Swift中,没有机制可以给这样的默认值。

#1


44  

Swift has this behaviour because of two phase initialisation. From Apple's Swift book:

Swift具有这种行为是因为两阶段初始化。迅速从苹果的书:

Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

Swift中的类初始化是一个两阶段的过程。在第一个阶段中,每个存储属性由引入它的类分配一个初始值。一旦确定了每个存储属性的初始状态,第二个阶段就开始了,每个类都有机会在考虑使用新实例之前进一步定制其存储属性。

Class need some kind of default value before first phase ends. Customising values is part of second phase.

类在第一阶段结束之前需要某种默认值。定制值是第二阶段的一部分。

Objective-C didn't had this behaviour, because it could always give 0 as default for primitives and nil for objects, but in Swift there is no mechanism to give such default value.

Objective-C没有这种行为,因为对于原语,它总是可以给0作为默认值,对于对象,它可以给nil,但是在Swift中,没有机制可以给这样的默认值。