I need to randomize an array for an app I am trying to build in swift, and to test the function I created I put it into a playground with a sample array. I realize that this question has been asked here: How do I shuffle an array in Swift? but I was just wondering why this specific way is not working. Here is my code:
import UIKit
var arrayOne : [String] = ["", "", "", "", ""]
func randomizeArray(array : [String]) -> [String] {
var randomizedArray : [String] = []
var copyOfArray : [String] = array
repeat {
let arrayCount : Int = array.count - 1
let randomElement : Int = Int(arc4random_uniform(UInt32(arrayCount)))
let arraySlice : String = array[randomElement]
randomizedArray.append(arraySlice)
copyOfArray.remove(at : randomElement)
} while array.count > 0
return randomizedArray
}
print(randomizeArray(array : arrayOne))
When I try to print the function using array one, it says "Fatal error: index is out of range." Any ideas why this happened?