Swift 3中的Int和Self.Index.Distance冲突

Swift 3中的Int和Self.Index.Distance冲突,swift,swift3,Swift,Swift3,我有: extension-MutableCollection,其中Index==Int{//shuffle self-in-place的元素 变异func shuffleInPlace(){ 如果计数

我有:

extension-MutableCollection,其中Index==Int{//shuffle self-in-place的元素
变异func shuffleInPlace(){
如果计数<2{return}//则空集合和单元素集合不洗牌
对于0中的i..<计数-1{
设j=Int(arc4random_均匀(UInt32(count-i))+i
守卫i!=j else{continue}
交换(&self[i]、&self[j])
...
...
我得到了一个错误:


二进制运算符二进制运算符“…试试这个,将
count-1括在括号中:

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 ] )
            ...

...
用于0中的i.<(计数-1)

Dave回答了这个问题,然后他的答案消失了!这是将count-1括在括号中:(count-1),我验证了它的有效性。谢谢,Dave,很抱歉我无法对你的答案进行投票:(我想你可能在这里有答案:
Int(arc4random_uniform(UInt32(count-1)))
听起来像count-1需要是Int类型。是的,我删除了,因为我意识到我不知道count=p的类型以及我的答案是否有助于改变它的类型。我取消了删除…如果您的代码来自,请注意它已经用Swift 3版本更新。这是非常优越的,因为它考虑了数组切片,应该在我使用时使用nstead.@DaveThomas:我之所以链接到这个问题,是因为这是一个相同的问题。Nate Cook在后来添加了Swift 3版本,这也适用于切片,甚至适用于未按
Int
索引的集合。
for i in 0 ..< (count - 1)