Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

ios - Why do I get a "Variable used before being initialized" error on the line that I initialise the variable in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...