问题描述
我正在努力理解为什么我在使用Swift的iOS项目中遇到此编译器错误.如果我创建以下类:
I'm struggling to understand why I'm getting this compiler error in an iOS project using Swift. If I create the following class:
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
}
}
在self.c = self.runCalculation()
行上出现编译器错误,提示初始化前使用了变量'self.c'".
I get a compiler error on the line self.c = self.runCalculation()
saying "Variable 'self.c' used before being initialized".
起初我以为是因为编译器无法验证runCalculation()
方法没有访问self.c
,但是后来我尝试将init方法混为一谈:
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:
init () {
self.a = 3
self.c = self.runCalculation()
self.b = 4
}
,这次错误是在初始化之前使用了变量'self.b'"(在同一self.runCalculation()
行上).这表明编译器 能够检查方法访问的属性,据我所知,初始情况应该没有问题.
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.
当然,这是一个简单的示例,我可以轻松地进行重构以避免调用计算方法,但是在实际的项目中,可能会有多个计算,每个计算都可能涉及到很多.我希望能够分离出逻辑以保持事物的可读性.
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()
}
(或使用属性初始化程序let c = 0
),但我想了解为什么编译器在第一个示例中有问题.我是否缺少某些东西还是不必要的限制?
(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?
推荐答案
Swift具有这种行为,原因是两阶段初始化.摘自Apple的Swift书:
Swift has this behaviour because of two phase initialisation. From Apple's Swift book:
在第一阶段结束之前,类需要某种默认值.自定义值是第二阶段的一部分.
Classes need some kind of default value before the first phase ends. Customising values is part of the second phase.
Objective-C没有这种行为,因为它始终可以将0
设置为基元的默认值,将nil
设置为对象的默认值,但是在Swift中,没有机制可以提供这样的默认值.
Objective-C didn't have 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 a default value.
这篇关于为什么我会收到“初始化前使用变量"的信息?我在Swift中初始化变量的那一行错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!