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

后端 未结 1 601
自闭症患者
自闭症患者 2020-11-29 11:15

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

extension Array {
    var shuffle:[Element] {
              


        
1条回答
  •  再見小時候
    2020-11-29 12:17

    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.. [[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
        }
    }
    

    0 讨论(0)
提交回复
热议问题