0

I was working on something and wondering if I could shuffle an array of images without using .shuffle in Swift.

Here's my array:

var deck: [UIImage] = [UIImage(named: "2c.png")!, UIImage(named: "2h.png")!, UIImage(named: "2d.png")!, UIImage(named: "2s.png")!, UIImage(named: "3c.png")! ]
newCoder
  • 9
  • 2

1 Answers1

0

Yes, you can achieve your own custom shuffle method for Collection type.

For example:

extension Array {
    var yourShuffled: [Element] {
        return map{ (arc4random(), $0) }
               .sorted{ $0.0 < $1.0 }
               .map{ $0.1 }
    }
}

copied from leetcode

dengApro
  • 3,848
  • 2
  • 27
  • 41