I believe that with SwiftyJSON
to get a JSON
object to the array type in swift you should be doing
datas["products"].array or datas["products"].arrayValue
Are you extending the array class to have a shuffle method in the first place? If not, you could do something like this
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}
extension MutableCollectionType where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
guard count >= 2 else { return }
for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
Source . Differences: If
statement changed to guard
.
You could then do something like this
let shuffled = (datas["products"].array!).shuffle()
Or if you are okay using iOS 9 APIs, you can do the following without any extensions:
let shuffled = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(datas["products"].array!)