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
}
}
I get a compiler error on the line self.c = self.runCalculation()
saying "Variable 'self.c' used before being initialized".
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
}
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()
}
(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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…