Swift Error: Variable used within its own initial value

Francescu picture Francescu · Jun 5, 2014 · Viewed 53.8k times · Source

When I'm initializing an instance of an entity I'm getting the error Variable used within its own initial value.

Here is the code throwing the error:

class func buildWordDefinition (word:String, language:Language, root:TBXMLElement) -> WordDefinition
    {
        let word = WordDefinition(word: word, language: language)

The error points at the word variable.

Here is the WordDefinition class:

class WordDefinition {
    let word: String
    let language: Language

    init(word: String, language:Language)
    {
        self.word = word
        self.language = language
    }
}

What does this error mean ?

Answer

Cezar picture Cezar · Jun 5, 2014

You are declaring a constant named word, and trying to use the argument with the same name to initialize it. The compiler tries to use the just declared constant to assign its own initial value, instead of using the argument.