I am new to Swift and I am looking to go through an entire list of strings with no repeating elements.
var dare = ["do1", "do2", "do3", "do4", "do5"]
func dareTapped(){
var randomNumber = Int.random(in: 0..<5)
text.text = dare[randomNumber]
}
This gives me random strings but they repeat a bit too often.
2
Answers
You can make a copy, shuffle and get the last element like:
To repeat this process, you can do something like:
Usage:
This is a slightly different approach.
A second temporary array is needed.
dare
totempDare
and calls itself a second time.tempDare
array. Then it removes the item from the array and displays it simultaneously.When the array runs out of elements the cycle starts over.
PS: Joakim, I apologize for reopening the question