斯威夫特2:指数超出范围

时间:2016-03-28 10:22:07

标签: arrays button random indexoutofboundsexception

我正在创建一个包含两个阵列图像的快照游戏,当玩家匹配阵列的最后一个元素时,我得到错误"索引超出范围"。我怎么能阻止这个?也许有一条消息说“#34;所有的卡片都匹配了”#34; 提前致谢

我的卡阵列:

    var cardArray = ["angry", "apple", "boots","heart", "pumpkin", "rainbow", "sad", "acorn", "chestnuts"]
    var cardArray2 = ["enfadadoTxt", "manzanaTxt", "botasTxt", "corazonTxt", "calabazaTxt", "arcoirisTxt", "tristeTxt", "bellotaTxt", "castanaTxt"]


    @IBAction func playRoundTapped(sender: UIButton) {

    //playRoundButton.userInteractionEnabled = false

    firstRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count)
    firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])

    secondRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray2.count)
    secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])



}


func getRandomIntFromArray(Array: [String]) -> Int {

    return GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count)

}

这就是崩溃的地方:

@IBAction func snapButtonTapped(sender: UIButton) {

    if cardArray.count > 0 {

        if firstRandomNumber == secondRandomNumber {

            self.playerScore += 1
            self.playerScoreLabel.text = String(self.playerScore)

            cardArray.removeAtIndex(firstRandomNumber)
            cardArray2.removeAtIndex(secondRandomNumber)

            firstRandomNumber = getRandomIntFromArray(cardArray)
            secondRandomNumber = getRandomIntFromArray(cardArray2)

            firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])//The problem might be here
            secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])

            print(cardArray)
            print(cardArray2)


        } else {

            animationView.startCanvasAnimation()
            print("no match")


        }

    }

}

当我打印firstRandom编号,secondRandomNumber,array1和array2

0 1 ["愤怒","南瓜","伤心","栗子"] [" enfadadoTxt"," calabazaTxt"," tristeTxt"," castanaTxt"]

1 1 ["愤怒","伤心","栗子"] [" enfadadoTxt"," tristeTxt"," castanaTxt"]

0 0 ["愤怒","栗子"] [" enfadadoTxt"," castanaTxt"]

0 0 ["栗子"] [" castanaTxt"]

83844566 929875342 致命错误:索引超出范围

1 个答案:

答案 0 :(得分:1)

我现在唯一能看到的问题是:

firstRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count)
    firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])

    secondRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray2.count)
    secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])

将其更改为

firstRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count - 1)
    firstCardImageView.image = UIImage(named: cardArray[firstRandomNumber])

    secondRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray2.count - 1)
    secondCardImageView.image = UIImage(named: cardArray2[secondRandomNumber])

另外,

return GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count)

return GKRandomSource.sharedRandom().nextIntWithUpperBound(cardArray.count - 1)

我的逻辑背后是你的array.count返回9个元素,但数组从0开始。