Created
June 24, 2017 20:49
-
-
Save illescasDaniel/9f0e4046dc61dbfb426f05cfc2e1fece to your computer and use it in GitHub Desktop.
Shuffle Collection [Swift]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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