Two arrays
let array1 = ["A","B","C","D","E","F","G"]
let array2 = ["a","b","c","d","e","f","g"]
I want to choose an index from array2 and replace the object in array1 at the same index with the object from array2. For example
array1[3] = array2[3] //["A","B","C","d","E","F","G"]
I want to do this randomly, for example
let randomIndex = Int(arc4random_uniform(UInt32(Array2.count)))
I want to do this in a 'for' loop until all the indices and objects for array2 are used, but I don't want to repeat a randomIndex.
If I decrement the number of objects after each iteration, I still could get the same random index. If I use a Set of indices, and remove the used index, I lose my 'orderedness' (if that's a word).
So I seem to be stuck. BTW swift 4's .randomElement won't work on an array of Strings.
Any thoughts?
for object in array2 {
let randomIndex = Int(arc4random_uniform(UInt32(array2.count)))
array1[randomIndex] = array2[randomIndex]
array2.remove(at: randomIndex)
}
The above does NOT work as I desire. When the object is removed, a new order is established, and I can't replace the object in array1 at the appropriate indx.
I'm missing something obvious, but I'm not seeing it. Thanks in advance
Aucun commentaire:
Enregistrer un commentaire