0

After upgrading to Xcode 7.3 my Swift application keeps crashing when I execute a specific function.

For the code below I am told that "the 'var' parameters are deprecated and will be removed in Swift 3". The fix is to delete the var i.e. change "(var list: C)" to "(list: C)" but as soon as this is done I get an error five lines down for "swap(&list[i], &list[j])". I still need to shuffle the names (random draw) but unsure how to fix this?

// Shuffle array function
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
    let c = list.count
    if c < 2 { return list }
    for i in 0..<(c - 1) {
        let j = Int(arc4random_uniform(UInt32(c - i))) + i
        swap(&list[i], &list[j])
    }
    return list
}

I am guessing this is the main problem with my application.

Currently the app crashes when the shuffle function is called.

The app is available on the apple store here if you need more context: Shell-Out

Newby
  • 37
  • 1
  • 6
  • 1
    Note that http://stackoverflow.com/a/24029847/1187415 has been updated for Swift 2.0. – Martin R May 01 '16 at 06:05
  • 1
    And the reason for the crash is that you cannot `swap` an element with itself anymore: http://stackoverflow.com/questions/32689753/fatal-error-swapping-a-location-with-itself-is-not-supported-with-swift-2-0. – Martin R May 01 '16 at 06:12
  • Guys marking it as a duplicate I get but I was unable to apply the logic of the duplicate post to get my code working :( – Newby Jun 01 '16 at 14:01

0 Answers0