Search code examples
iosswiftvarlet

Expression resolves to unused I Value


I am new to Swift and I am trying to change background colour of button.

I am using chicken1 to change the background colour of the button all together

But I keep getting this error

Expression resolves to unused I Value"

Here is the current code.

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

Also should I use 'var' insted?


Solution

  • AFAIK, this isn't possible the way you are trying to do it.

    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.

    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.