arrays - 防止在循环遍历另一个数组时将重复对象存储在数组中

标签 arrays swift duplicates

我已经设法让我的代码随机选择对象并在我的 View Controller 上显示图像,但有时随机选择的对象将是重复的,所以我需要能够从中删除存储在第二个数组中的选定对象第一个数组,所以它们不能被第二次选择,但我不确定如何在我的代码中实现这一点,这是我的代码有效但可能会选择重复项:

@IBAction func drawCardsButtonPressed(_ sender: Any) {

    cardsDrawnArray = []

    if cardsDrawn == 1 {

        let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
        cardsDrawnArray.append(randomCards.tarotImage)

        tarotCardFive.image = cardsDrawnArray[0]

    } else if cardsDrawn == 3 {

        for _ in 0...2 {

            let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
            cardsDrawnArray.append(randomCards.tarotImage)

        }

        tarotCardFour.image = cardsDrawnArray[0]
        tarotCardFive.image = cardsDrawnArray[1]
        tarotCardSix.image = cardsDrawnArray[2]

    }

    else if cardsDrawn == 5 {

        for _ in 0...4 {

            let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
            cardsDrawnArray.append(randomCards.tarotImage)
        }

        tarotCardTwo.image = cardsDrawnArray[0]
        tarotCardFour.image = cardsDrawnArray[1]
        tarotCardFive.image = cardsDrawnArray[2]
        tarotCardSix.image = cardsDrawnArray[3]
        tarotCardEight.image = cardsDrawnArray[4]

    }  else if cardsDrawn == 9 {

        for _ in 0...8 {

            let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
            cardsDrawnArray.append(randomCards.tarotImage)
        }

        tarotCardOne.image = cardsDrawnArray[0]
        tarotCardTwo.image = cardsDrawnArray[1]
        tarotCardThree.image = cardsDrawnArray[2]
        tarotCardFour.image = cardsDrawnArray[3]
        tarotCardFive.image = cardsDrawnArray[4]
        tarotCardSix.image = cardsDrawnArray[5]
        tarotCardSeven.image = cardsDrawnArray[6]
        tarotCardEight.image = cardsDrawnArray[7]
        tarotCardNine.image = cardsDrawnArray[8]

    } else {
        print(" Invalid Number")
    }

}

非常感谢任何解决此问题的帮助。

最佳答案

与其每次都执行代码来随机获得一张牌,不如“洗牌”你的牌数组,然后按照洗牌后的顺序抽取它们。效率更高,而且您甚至不必考虑重复项。

基本上,如果您的数组是数字 1 到 10,它会像这样开始:

let cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

如果您运行一个循环,每次都选择一个“随机槽”,您将经常得到重复项(如您所见)。所以……

let cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let shuffledCards = myShuffleFunc(cards)

你最终会得到类似的东西:

[9, 2, 5, 6, 3, 1, 8, 10, 4, 7]

此时,您可以轻松地做到:

    tarotCardOne.image = shuffledCards[0]
    tarotCardTwo.image = shuffledCards[1]
    tarotCardThree.image = shuffledCards[2]
    tarotCardFour.image = shuffledCards[3]
    tarotCardFive.image = shuffledCards[4]

或者您如何分配它们。

查看此帖子以获得一些好的改组代码:How do I shuffle an array in Swift?

关于arrays - 防止在循环遍历另一个数组时将重复对象存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44887450/

相关文章:

PHP:遍历数组并将客户依次分配给用户

Swift 泛型强制的误解

javascript - 如何从字符串中删除重复行?

javascript - 如何从在 javascript 中具有相同键的对象创建对象数组

java - 简单的校验和算法

Swift 尾随闭包无法编译

iOS:单击 TableCellView 内的 View

android - 如何确保解析数组时不会返回重复的 JSON 数据?

sql - 在Microsoft Access中使用查询来比较两个字段并查找多个匹配值

javascript - 从包含数组属性的基础对象有选择地创建一个新的对象数组