Swift:'使用未解析的标识符' - 如何更改变量并在不同的函数中使用该变量?

时间:2022-08-24 14:48:08

I need to have this button that I have created (a blue button) alter a variable (named playerMark) that I create and use earlier in another code block in my code.

我需要让我创建的这个按钮(一个蓝色按钮)改变我在代码中的另一个代码块中创建和使用的变量(名为playerMark)。

Here is where playerMark is introduced and is used:

以下是playerMark的介绍和使用方法:

func setImgforCircle(spot:Int, player:Int) {
        var playerMark = player == 1 ? "Red_x" : "Red_o"

    plays[spot] = player
    switch spot {
    case 1:
        circle1.image = UIImage(named: playerMark)
    case 2:
        circle2.image = UIImage(named: playerMark)
    case 3:
        circle3.image = UIImage(named: playerMark)
    case 4:
        circle4.image = UIImage(named: playerMark)
    case 5:
        circle5.image = UIImage(named: playerMark)
    case 6:
        circle6.image = UIImage(named: playerMark)
    case 7:
        circle7.image = UIImage(named: playerMark)
    case 8:
        circle8.image = UIImage(named: playerMark)
    case 9:
        circle9.image = UIImage(named: playerMark)

    default:
        circle2.image = UIImage(named: playerMark)
    }

}

I have an IBAction named blueBtnClicked hooked up to my blue button that I need to execute:

我有一个名为blueBtnClicked的IBAction连接到我需要执行的蓝色按钮:

playerMark = player == 1 ? "Blue_x" : "Blue_o"

but the console does not "remember" the variables (playerMark and player) and I get the error, 'use of unresolved identifier.' Here is the blueBtnClicked block:

但是控制台没有“记住”变量(playerMark和播放器),我得到了错误,'使用未解析的标识符'。这是blueBtnClicked块:

@IBAction func blueBtnClicked(sender: UIButton) {
        gameboard.image = UIImage(named: "Blue_gb")
        resultsView.image = UIImage(named: "Blue_results")
        colorsView.image = UIImage(named: "Blue_Colors")
        colorsBtn.setTitleColor(UIColor.lightTextColor(), forState: UIControlState.Normal)
        resetBtn.setTitleColor(UIColor.lightTextColor(), forState: UIControlState.Normal)
    }

What can I do to to alter the playerMark variable when certain buttons are clicked? Please explain all answers very thoroughly as I am new to Swift.

单击某些按钮时,我该怎么做才能改变playerMark变量?因为我是Swift的新手,请详细解释所有答案。

1 个解决方案

#1


1  

The problem is that you are trying to access a variable that is not in scope. Currently the scope of the playerMark variable is the setImgforCircle function, that means that playerMark is only accessible in setImgforCircle.

问题是您正在尝试访问不在范围内的变量。目前,playerMark变量的范围是setImgforCircle函数,这意味着playerMark只能在setImgforCircle中访问。

A solution can be to define the playerMark variable outside of the function as a class variable like so:

一个解决方案是将函数外部的playerMark变量定义为类变量,如下所示:

var playerMark: String?

The question mark makes this variable an optional so that you do not need to assign a value directly (the value is now nil)

问号使此变量成为可选项,因此您无需直接指定值(该值现在为零)

to assign a value to playerMark you can simply say

要为playerMark分配一个值,你可以简单地说

playerMark = player == 1 ? "Red_x" : "Red_o" 

you don't need to define it again with var.

你不需要再用var来定义它。

Now the var is available anywhere in your class. You can read it by using playerMark! The exclamation sign there means "I know that this value can be nil, but give me the value anyway".

现在var可以在你班级的任何地方使用。您可以使用playerMark阅读它!那里的惊叹号意味着“我知道这个价值可以是零,但无论如何都要给我价值”。

The safe way to handle nil values of playerMark would be:

处理playerMark的nil值的安全方法是:

if playerMark {
   //access playerMark!
} else {
   // playerMark has no valid value, do something else
}

To get a greater understanding of swift i recommend reading "The Swift Programming Language" by Apple available for free on iBooks

为了更好地理解swift,我建议在iBooks上免费阅读Apple的“The Swift Programming Language”

#1


1  

The problem is that you are trying to access a variable that is not in scope. Currently the scope of the playerMark variable is the setImgforCircle function, that means that playerMark is only accessible in setImgforCircle.

问题是您正在尝试访问不在范围内的变量。目前,playerMark变量的范围是setImgforCircle函数,这意味着playerMark只能在setImgforCircle中访问。

A solution can be to define the playerMark variable outside of the function as a class variable like so:

一个解决方案是将函数外部的playerMark变量定义为类变量,如下所示:

var playerMark: String?

The question mark makes this variable an optional so that you do not need to assign a value directly (the value is now nil)

问号使此变量成为可选项,因此您无需直接指定值(该值现在为零)

to assign a value to playerMark you can simply say

要为playerMark分配一个值,你可以简单地说

playerMark = player == 1 ? "Red_x" : "Red_o" 

you don't need to define it again with var.

你不需要再用var来定义它。

Now the var is available anywhere in your class. You can read it by using playerMark! The exclamation sign there means "I know that this value can be nil, but give me the value anyway".

现在var可以在你班级的任何地方使用。您可以使用playerMark阅读它!那里的惊叹号意味着“我知道这个价值可以是零,但无论如何都要给我价值”。

The safe way to handle nil values of playerMark would be:

处理playerMark的nil值的安全方法是:

if playerMark {
   //access playerMark!
} else {
   // playerMark has no valid value, do something else
}

To get a greater understanding of swift i recommend reading "The Swift Programming Language" by Apple available for free on iBooks

为了更好地理解swift,我建议在iBooks上免费阅读Apple的“The Swift Programming Language”