为随机颜色生成器获取EXC_BAD_INSTRUCTION

时间:2017-05-19 06:02:04

标签: arrays swift

我正在尝试创建一个随机颜色生成器,我应该选择正确的颜色。我的代码如下:

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)";
}

当我运行此代码时,它似乎为代码infoText.text = "Touch \(targetColor)";报告了一个主题1:EXC_BAD_INSTRUCTION。我尝试搜索类似的问题,并指出解开 nil 存在问题。但是,我的代码中没有 nil

我还是初学者你能解释一下我能做些什么来解决这个问题,以及我如何解决这些问题来解决这个问题?

谢谢!

0 个答案:

没有答案
相关问题