Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

swift2 - fatal error: swapping a location with itself is not supported with Swift 2.0

I have this extension which will create a new array which have group of arrays randomly from given array:

extension Array {
    var shuffle:[Element] {
        var elements = self
        for index in 0..<elements.count {
            swap(&elements[index], &elements[ Int(arc4random_uniform(UInt32(elements.count-index)))+index ])
        }
        return elements
    }
    func groupOf(n:Int)-> [[Element]] {
        var result:[[Element]]=[]
        for i in 0...(count/n)-1 {
            var tempArray:[Element] = []
            for index in 0...n-1 {
                tempArray.append(self[index+(i*n)])
            }
            result.append(tempArray)
        }

        return result
    }
}

And I am using it like this:

let mainArr = Array(1...60)
let suffeldArr = mainArr.shuffle.groupOf(10)
print(suffeldArr)

And it will print like:

[[10 random element between 1 to 60], [10 random element between 1 to 60], [10 random element between 1 to 60], [10 random element between 1 to 60], [10 random element between 1 to 60], [10 random element between 1 to 60]]

But it is giving me an error at run time at this line:

swap(&elements[index], &elements[ Int(arc4random_uniform(UInt32(elements.count-index)))+index ])

Which says:

fatal error: swapping a location with itself is not supported

It was working fine in 1.2 but now it is not working in 2.0.

I don't know how to solve this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to swap an element with itself, you will need to perform a check to see if you are not trying to swap an element to the same spot in the array, like so:

extension Array {
    var shuffle:[Element] {
        var elements = self
        for index in 0..<elements.count {
            let newIndex = Int(arc4random_uniform(UInt32(elements.count-index)))+index
            if index != newIndex { // Check if you are not trying to swap an element with itself
                swap(&elements[index], &elements[newIndex])
            }
        }
        return elements
    }
    func groupOf(n:Int)-> [[Element]] {
        var result:[[Element]]=[]
        for i in 0...(count/n)-1 {
            var tempArray:[Element] = []
            for index in 0...n-1 {
                tempArray.append(self[index+(i*n)])
            }
            result.append(tempArray)
        }

        return result
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...