0

I am trying to create a random color generator where I'm supposed to select the right color. My code is as follows:

var colors = ["Red", "Green", "Blue", "Dark"];

var targetColor = "";

@IBAction func touchTheColor(_ sender: Any) {

    if colors[(sender as AnyObject).tag] == targetColor {

        infoText.text = "You have touched the right color!"
    } else {
        infoText.text = "Please touch the \(targetColor) color"
    }


}



@IBAction func generateRandomcolor(_ sender: Any) {

    let index = Int(arc4random_uniform(UInt32(colors.count)));

    targetColor = colors[index];

    infoText.text = "Touch \(targetColor)";
}

When I run this code, it seems to report a Thread 1: EXC_BAD_INSTRUCTION, for the code infoText.text = "Touch \(targetColor)";. I tried searching on similar issues and it states that there is a problem with unwrapping nil. However, there isn't nil in my code.

I'm still a beginner could you explain to me what I could do to solve this and also how I could troubleshoot these problems to solve it myself?

Thank you!

Phang Weijun
  • 15
  • 1
  • 4

0 Answers0