简体   繁体   English

表达式解析为未使用的I值

[英]Expression resolves to unused I Value

I am new to Swift and I am trying to change background colour of button. 我是Swift的新手,我正在尝试更改按钮的背景颜色。

I am using chicken1 to change the background colour of the button all together 我正在使用chicken1来一起更改按钮的背景颜色

But I keep getting this error 但我一直收到这个错误

Expression resolves to unused I Value" 表达式解析为未使用的I值“

Here is the current code. 这是当前的代码。

let chicken1 = button1; button2
chicken1.backgroundColor = UIColor.blueColor()

Also should I use 'var' insted? 我也应该使用'var'insted吗?

AFAIK, this isn't possible the way you are trying to do it. AFAIK,这不可能像你想要的那样。

One option is to loop through all of your buttons and change the background color in the loop. 一种选择是遍历所有按钮并更改循环中的背景颜色。

for button in [button1, button2]{
    button.backgroundColor = UIColor.blueColor()
}

The reason you are getting this error is because this line 你得到这个错误的原因是因为这一行

let chicken1 = button1; button2

is the same as 是相同的

let chicken1 = button1
button2 //this value isn't used

Swift doesn't actually need ; 斯威夫特实际上并不需要; at the end of a line like in other languages. 像在其他语言中一样在一行的末尾。 By adding ; 通过添加; , you tell Swift that you want to have multiple expressions on a single line. ,你告诉Swift你想在一行上有多个表达式。

The second line doesn't do anything because there are no function calls and no assignment, so it's like you want to get the value of button2 but you don't use it. 第二行没有任何事情,因为没有函数调用而没有赋值,所以就像你想获得 button2的值但你不使用它。

As Arc676 mention into comment you can't assign multiple values to a single variable. 由于Arc676提到注释,您无法为单个变量分配多个值。

So first way to achieve that is you can set background color individually as shown below: 因此,实现这一目标的第一种方法是您可以单独设置背景颜色,如下所示:

button1.backgroundColor = UIColor.blueColor()
button2.backgroundColor = UIColor.blueColor()

Or if you have array of button: 或者如果你有按钮数组:

let chicken1 = [button1, button2]
for item in chicken1 {

    item.backgroundColor = UIColor.blueColor()
}

for Swift 3 Xcode 8 use the code below 对于Swift 3 Xcode 8,请使用以下代码

button1.backgroundColor = UIColor.blue
button2.backgroundColor = UIColor.blue

Or if you have array of button: 或者如果你有按钮数组:

let chicken1 = [button1, button2]
for item in chicken1 {
    item.backgroundColor = UIColor.blue
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM