Swift Error:变量在其自己的初始值内使用 [英] Swift Error: Variable used within its own initial value

查看:1169
本文介绍了Swift Error:变量在其自己的初始值内使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我初始化实体的实例时,我收到错误在其自己的初始值中使用的变量。

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)

错误指向变量。

这是WordDefinition类:

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 ?

推荐答案

您正在声明一个名为 word 的常量,并尝试使用具有相同名称的参数以初始化它。编译器尝试使用刚刚声明的常量来分配自己的初始值,而不是使用参数。

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.

这篇关于Swift Error:变量在其自己的初始值内使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆