Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a "Variable used before being initialized" error on the line that I initialise the variable in Swift?

Tags:

ios

swift

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?

like image 328
rankAmateur Avatar asked Nov 20 '14 11:11

rankAmateur


1 Answers

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

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.

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

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.

like image 51
Kirsteins Avatar answered Sep 21 '22 22:09

Kirsteins