0

I'm using the array shuffle function from here: http://iosdevelopertips.com/swift-code/swift-shuffle-array-type.html.

On this line:

for var index = array.count - 1; index > 0; index -= 1   

in the code below

func shuffleArray<T>( arrayparam: Array<T>) -> Array<T>
{
    var array = arrayparam
    for var index = array.count - 1; index > 0; index -= 1
    {
        // Random int from 0 to index-1
        let j = Int(arc4random_uniform(UInt32(index-1)))

        // Swap two array elements
        // Notice '&' required as swap uses 'inout' parameters
        swap(&array[index], &array[j])
    }
    return array
}

Swift throws this warning:

C-style for statement is deprecated and will be removed in a future version of Swift

There isn't any recommendation of what should be used here. Any ideas what should replace it?

4thSpace
  • 43,672
  • 97
  • 296
  • 475

4 Answers4

2

take a look at http://bjmiller.me/post/137624096422/on-c-style-for-loops-removed-from-swift-3

only decrease by 1:

for i in (0...n).reverse() {

}

or

decrease by more steps:

for i in someNum.stride(through: 0, by: -2)  {

}

More info: there are two versions for stride: through and to. Difference is <= and >= for through, while < and > for to, depending on what you need.

Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • This isn't working so I posted the whole thing in the OP. – 4thSpace Mar 25 '16 at 03:59
  • unless you post your real code and tell what does not working, or I don't believe it. – Wingzero Mar 25 '16 at 04:04
  • @4thSpace `Int(arc4random_uniform(UInt32(index-1)))` will always crash. try not subtracting 1. You need also to add a guard statement to make sure `index != j` before using swap – Leo Dabus Mar 25 '16 at 04:14
  • @LeoDabus can you post what that will look like? guard has to be used within a function right? I'm already in a function so I'm not sure how that will work. – 4thSpace Mar 25 '16 at 14:00
1
func shuffle<T>(array: Array<T>) -> Array<T> {
    var result = array
   for index in array.indices.reverse() {
        // generate random swapIndex and add a where clause
        // to make sure it is not equal to index before swaping
        guard
            case let swapIndex = Int(arc4random_uniform(UInt32(array.count - index))) + index
            where index != swapIndex
            else { continue }
        swap(&result[index], &result[swapIndex])
    }
    return result
}

var arrInt = Array(1...100)
shuffle(arrInt)  // [28, 19, 25, 53, 35, 60, 14, 62, 34, 15, 81, 50, 59, 40, 89, 30, 2, 54, 27, 9, 82, 21, 11, 67, 84, 75, 44, 97, 66, 83, 36, 20, 26, 1, 76, 77, 8, 13, 72, 65, 64, 80, 88, 29, 98, 37, 33, 70, 52, 93, 100, 31, 4, 95, 45, 49, 61, 71, 24, 16, 12, 99, 94, 86, 46, 69, 63, 22, 48, 58, 51, 18, 43, 87, 41, 6, 92, 10, 38, 23, 68, 85, 42, 32, 55, 78, 56, 79, 3, 47, 39, 57, 90, 17, 5, 73, 7, 91, 74, 96]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0
for var index = array.count - 1; index > 0; index -= 1

Use a reverse range. Form the range and reverse it:

for index in (1..<array.count).reverse

However, as discussed in my answer here, there's a nicer way; I provide a >>> operator so you can say

for index in array.count>>>1
Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
-2

why don't you try:

for var index = array.count - 1; index > 0; index =index-1 ; 
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
slate24
  • 36
  • 3