-2

I've found this code to shuffle an array:

func shuffle<T>(inout array: [T]) {
    for i in 1..<array.count {
        let j = Int(arc4random_uniform(UInt32(i)))
        (array[i], array[j]) = (array[j], array[i])
    }
}

which works fine with

var arr = [1,2,3,4]
shuffle(&arr)
println(arr)

But how do I use with a NSMutableArray? I've tried

 var PicturesArray :NSMutableArray = []

 shuffle(PicturesArray)as Array
 shuffle([PicturesArray])
 shuffle(PicturesArray[])

but can't find any answers probably something I'm doing silly thanks for looking

Rob
  • 415,655
  • 72
  • 787
  • 1,044
user2164327
  • 283
  • 1
  • 2
  • 13
  • See https://gist.github.com/robertmryan/e11a9fd983a115074b8e for example of Fisher-Yates shuffle of `NSMutableArray` in Swift. – Rob Mar 22 '15 at 12:57

1 Answers1

0

You have to cast it.

var arr = NSMutableArray(array: [1,2,3,4,5])
var arr_ = arr as [AnyObject]

shuffle(&arr_)

shuffle(PicturesArray)as Array // This is syntax error
shuffle([PicturesArray])       // This will make a new array with only one object inside
shuffle(PicturesArray[])       // This is also syntax error
Arbitur
  • 38,684
  • 22
  • 91
  • 128