Dribbler Dribbler - 1 month ago 32
Swift Question

Int and Self.Index.Distance conflict in Swift 3

I have:

extension MutableCollection where Index == Int { // shuffle elements of self in place

mutating func shuffleInPlace() {

if count < 2 { return } // empty and single-element collections don't shuffle

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

...


and I'm getting the error:


Binary operator Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'


Does anyone know how to rectify this?

Answer

Try this instead, wrap count -1 in parenthesis :

for i in 0 ..< (count - 1)
Comments