Skip to content

Instantly share code, notes, and snippets.

@illescasDaniel
Created June 24, 2017 20:49
Show Gist options
  • Select an option

  • Save illescasDaniel/9f0e4046dc61dbfb426f05cfc2e1fece to your computer and use it in GitHub Desktop.

Select an option

Save illescasDaniel/9f0e4046dc61dbfb426f05cfc2e1fece to your computer and use it in GitHub Desktop.
Shuffle Collection [Swift]
import GameplayKit
extension Collection {
func shuffled() -> [Iterator.Element] {
let shuffledArray = (self as? NSArray)?.shuffled()
let outputArray = shuffledArray as? [Iterator.Element]
return outputArray ?? []
}
mutating func shuffle() {
if let selfShuffled = self.shuffled() as? Self {
self = selfShuffled
}
}
}
var numbers = [1,2,3,4,5]
numbers.shuffle()
print(numbers)
print([10, "hi", 9].shuffled())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment