数组循环的swift Xcode

数组循环的swift Xcode,swift,xcode,loops,for-loop,Swift,Xcode,Loops,For Loop,我确实有下一个代码来完成它的工作,但我正在寻找一种使它更优雅的方法 var collectionAnswers = [0,0,1] if collectionAnswers[0] == 1 { button1Oulet.backgroundColor = UIColor.green } else { button1Oulet.backgroundColor = UIColor.red } if collectionAnswers[1

我确实有下一个代码来完成它的工作,但我正在寻找一种使它更优雅的方法

var collectionAnswers = [0,0,1]

if collectionAnswers[0] == 1 {

        button1Oulet.backgroundColor = UIColor.green
    } else {

        button1Oulet.backgroundColor = UIColor.red
    }

    if collectionAnswers[1] == 1 {

        button2Oulet.backgroundColor = UIColor.green
    } else {

        button2Oulet.backgroundColor = UIColor.red
    }

    if collectionAnswers[2] == 1 {

        button3Oulet.backgroundColor = UIColor.green
    } else {

        button3Oulet.backgroundColor = UIColor.red
    }
到目前为止,我已经带来了下一个代码,但我无法使它工作

你能帮帮我吗,有点困在这里了

for (index,element) in collectionAnswers.enumerated() {

        switch index {
        case 0, 1, 2:
            if element == 0 {print("Bad")}
            else {

                for button in collectionOfButtons {

                    if index == button.tag && element == 1 {

                        button.backgroundColor = UIColor.green

                    } else {

                        button.backgroundColor = UIColor.red
                    }
                }

                print("OK")
            }
        default:
            break
        }
    }

谢谢你的帮助

长话短说,创建一个包含按钮ou(t)let的附加数组


我投票结束这个问题,因为它要求使一些工作代码更加优雅。这是代码审查网站的主题。忘记if-then-else。使用切换语句。谢谢,伙计,,,工作完美,,,我真的很感谢你的帮助
let collectionAnswers = [0, 0, 1]
let buttons = [button1Oulet, button2Oulet, button3Oulet]

for (index, answer) in collectionAnswers.enumerated() {
    buttons[index].backgroundColor = (answer == 0) ? .red : .green
}