I agree with Ahmad F that recursion is probably the way to go. My example is solution is in the same ballpark, with the difference that I pass the index to avoid having a class-wide variable just for this purpose. (+ some minor stylistical differences).
func playSound(fromIndex index: Int = 0)) {
guard index < arrayOfOptions.count else { return } // safety first
if arrayOfOptions.contains(index) {
// play a sound
}
guard index <= 60 else { return }
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
self.playSound(fromIndex: index+1)
}
}
This would simply be started with playSound()
@PurplePanda If the idea is to run through the entire array, changing the guard statement to guard index < arrayOfOptions.count else { return } would be more sensible. That is also why I have added the initial guard statement, since there is no technical reason to assume the array isn't empty...
It might easily make sense to expand upon this and having the function receive the array as well, but that depends on your specific requirements obviously...
arrayOfOptionsthat has to do with the playback? (Do you actually use the values to differentiate sounds for instance?)