Arrays 如何从数组中洗牌文本而不重复一个单词并遍历整个数组?(Swift)

Arrays 如何从数组中洗牌文本而不重复一个单词并遍历整个数组?(Swift),arrays,swift,loops,random,shuffle,Arrays,Swift,Loops,Random,Shuffle,我试图洗牌我的数组并显示所有的单词,而不重复它们。我试了一会儿,打了个圈。我想我可以使用枚举,但它并不完全有效,不知道为什么…有什么想法吗 我的阵列 var fruitOptions = [ Fruit(id: 1, fruit:"If I understood it correctly you shouldn't shuffle your collection every time you call nextFruit. Just shuff

我试图洗牌我的数组并显示所有的单词,而不重复它们。我试了一会儿,打了个圈。我想我可以使用枚举,但它并不完全有效,不知道为什么…有什么想法吗

我的阵列

    var fruitOptions = [
        
        Fruit(id: 1, fruit:"If I understood it correctly you shouldn't shuffle your collection every time you call 
nextFruit
. Just shuffle it once before iterating your collection. The simplest solution in my opinion is to store the index of the current fruit. In your next fruit method check if the index is equal to zero and if true shuffle your fruits. Get the fruit at the current index and increment it. If the index is equal to the number of fruits just reset it to zero. Something like:

struct Fruit {
    let id: Int
    let fruit: Character
    let name: String
}
var选项=[

水果(id:1,水果:"如果我理解正确,你就不应该每次调用
nextFruit
时都洗牌你的集合。只需在迭代你的集合之前洗牌一次。我认为最简单的解决方案是存储当前水果的索引。在下一个水果方法中,检查索引是否等于零,如果为真,洗牌你的水果。获取t他在当前索引处计算结果并递增。如果该索引等于结果数,只需将其重置为零。类似于:


操场测试:

var选项=[

水果(id:1,水果:"抱歉,我不确定你的实际目标是什么。你能详细说明一下吗?我想能够洗牌,显示1,然后遍历数组。例如,当数字5显示为洗牌项目时,它遍历所有9个项目,然后停止。此时它重复单词,而不是遍历所有单词。所以只需将其洗牌一次,从开始调用nextFruit时,startIndex和索引只需递增即可。只有当索引为0时,才能对其进行洗牌