kye kye - 5 months ago 203
Swift Question

Shuffle array swift 3

How can I convert the function below to to

swift 3
? Currently getting a
Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'
error.

extension MutableCollection where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
if count < 2 { return }

for i in 0..<count - 1 { //error takes place here
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}


reference: http://stackoverflow.com/a/24029847/5222077

Answer

Use startIndex and endIndex instead of 0 and count:

extension MutableCollection where Index == Int {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffleInPlace() {
        // empty and single-element collections don't shuffle
        if count < 2 { return }

        for i in startIndex ..< endIndex - 1 {
            let j = Int(arc4random_uniform(UInt32(endIndex - i))) + i
            guard i != j else { continue }
            swap(&self[i], &self[j])
        }
    }
}

Another advantage is that this also works correctly with array slices (where the first index is not necessarily zero).

Comments