如何在不重复单词并遍历整个数组的情况下从数组中随机播放文本? (Swift)

How to shuffle text from array without repeating a word and going through the whole array? (Swift)

我正在尝试打乱我的数组并显示所有单词而不重复它们。我尝试了一段时间并循环。我以为我可以使用枚举,但它并不完全有效,不知道为什么...有什么想法吗?

我的数组

    var fruitOptions = [
        
        Fruit(id: 1, fruit:"", name: "KIWI"),
        Fruit(id: 2, fruit:"", name: "APPLE"),
        Fruit(id: 3, fruit:"", name: "PEAR"),
        Fruit(id: 4, fruit:"", name: "ORANGE"),
        Fruit(id: 5, fruit:"", name: "STRAWBERRY"),
        Fruit(id: 6, fruit:"", name: "WATERMELON"),
        Fruit(id: 7, fruit:"", name: "GRAPES"),
        Fruit(id: 8, fruit:"", name: "BANANA"),
        Fruit(id: 9, fruit:"", name: "CHERRY")
        
    ]

随机显示下一个水果的函数

 mutating func nextFruit() {
        
        fruitOptions.shuffle()
        
        while fruitOptions.count <= 8 {
            if fruitNumber + 1 < fruitOptions.count {
            
            
              //  if !fruitOptions.contains(fruitOptions.capacity) {
           
                fruitNumber += 1
            
//
//            for (index, fruitOptions) in fruitOptions.enumerated() {
//                if case fruitOptions.id = index + 0 {
//                    fruitNumber += 1
                    
                          
                } else {
                    
                    // fruitNumber = 0     //  <- Makes a never ending app.!? :/
                    score = 0
                }
            }
        }
    }

如果我没理解错的话,你不应该在每次调用 nextFruit 时都洗牌 collection。在迭代 collection 之前只需将其洗牌一次。我认为最简单的解决方案是存储当前水果的索引。在您的下一个水果方法中,检查索引是否等于零,如果为真,则洗牌您的水果。获取当前索引处的水果并增加它。如果索引等于水果的数量,则将其重置为零。类似于:

struct Fruit {
    let id: Int
    let fruit: Character
    let name: String
}

游乐场测试:

var fruitOptions = [
    Fruit(id: 1, fruit:"", name: "KIWI"),
    Fruit(id: 2, fruit:"", name: "APPLE"),
    Fruit(id: 3, fruit:"", name: "PEAR"),
    Fruit(id: 4, fruit:"", name: "ORANGE"),
    Fruit(id: 5, fruit:"", name: "STRAWBERRY"),
    Fruit(id: 6, fruit:"", name: "WATERMELON"),
    Fruit(id: 7, fruit:"", name: "GRAPES"),
    Fruit(id: 8, fruit:"", name: "BANANA"),
    Fruit(id: 9, fruit:"", name: "CHERRY")]

var index = 0

func nextFruit() {
    if index == 0 { fruitOptions.shuffle() }
    print(fruitOptions[index])
    index += 1
    if index == fruitOptions.count  {
        print("end of fruits")
        index = 0
    }
}

nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()

这将打印:

Fruit(id: 5, fruit: "", name: "STRAWBERRY")
Fruit(id: 2, fruit: "", name: "APPLE")
Fruit(id: 4, fruit: "", name: "ORANGE")
Fruit(id: 9, fruit: "", name: "CHERRY")
Fruit(id: 3, fruit: "", name: "PEAR")
Fruit(id: 6, fruit: "", name: "WATERMELON")
Fruit(id: 8, fruit: "", name: "BANANA")
Fruit(id: 1, fruit: "", name: "KIWI")
Fruit(id: 7, fruit: "", name: "GRAPES")
end of fruits
Fruit(id: 2, fruit: "", name: "APPLE")
Fruit(id: 3, fruit: "", name: "PEAR")
Fruit(id: 1, fruit: "", name: "KIWI")
Fruit(id: 4, fruit: "", name: "ORANGE")