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
Checking If an Object Is a Given Type in Swift
How to Detect If an Skspritenode Has Been Touched
How to Create a Segue That Can Be Called from a Button That Is Created Programmatically
Programmatically Detect Tab Bar or Tabview Height in Swiftui
How to Check If a Firebase Database Value Exists
Get a Swift Variable'S Actual Name as String
How to Disable Pasting in a Textfield in Swift
Getting Keyboard Size from Userinfo in Swift
How to Compare Two Dictionaries in Swift
How to Create Array of Unique Object List in Swift
Removeobjectsatindexes For Swift Arrays
Express For Loops in Swift With Dynamic Range
Choosing Coredata Entities from Form Picker
Generic Function Taking a Type Name in Swift
How to Create a Reference Cycle Using Dispatchqueues