Fatal Error: Swapping a Location With Itself Is Not Supported With Swift 2.0

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

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

Swift - trying to use a shuffle function and get fatal error: swapping a location with itself is not supported

From the error description, you should change your shuffle call as:

func shuffle<C: MutableCollection>( list: C) -> C where C.Index == Int {
var list = list
let count = sourceDays.count
for i in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
if i != j {
swap(&list[i], &list[j])
}
}
return list
}

You are trying to shuffle an index with itself and in this case i and j both had same value. Hence the error.

How to exchange elements in swift array?

Use swap:

var cellOrder = [1,2,3,4]
swap(&cellOrder[0], &cellOrder[1])

Alternately, you can just assign it as a tuple:

(cellOrder[0], cellOrder[1]) = (cellOrder[1], cellOrder[0])

Fatal Error while changing app icons in SwiftUI 2.0

In place of Settings view creation you must provide environmentObject, like

Settings().environmentObject(IconNames())

Thread 1: Fatal error: Index out of range, return array.count + 1

Assuming you have some piece of data in posts[0], you are never actually displaying it. For indexPath.row = 0, you are displaying a profile cell, and then you start displaying the data from posts[1] and on. Change your problem line to:
let post = posts[indexPath.row - 1]

Fatal error when changing all annotations' image in a MKMapView

viewForAnnotation returns an optional, according to the documentation:

Return Value

The annotation view or nil if the view has not yet been
created. This method may also return nil if the annotation is not in
the visible map region and therefore does not have an associated
annotation view.

It returns even nil if the annotation is not in the visible area of your map so iterating over a loop of all annotations is not the right approach to change the annotation images.

You have to implement

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {}

of the MKMapViewDelegate protocol and create the annotation view there.

I have created one string extension to join string with space in swift 2.0 but its not working in swift 2.1 xcode 7

the error you receive is self-explanatory. by the way, there is easy to do the same without any complication ...

let arr = ["one","two","three"]
let str = arr.joinWithSeparator(" ") // "one two three"


Related Topics



Leave a reply



Submit