I have an array like:
var names: String = [ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ]
I would like to get 3 random elements from that array. I'm coming from C# but in swift I'm unsure where to start. I think I should shuffle the array first and then pick the first 3 items from it for example?
I tried to shuffle it with the following extension:
extension Array
{
mutating func shuffle()
{
for _ in 0..<10
{
sort { (_,_) in arc4random() < arc4random() }
}
}
}
but it then says "'()' is not convertible to '[Int]'" at the location of "shuffle()".
For picking a number of elements I use:
var randomPicks = names[0..<4];
which looks good so far.
How to shuffle? Or does anyone have a better/more elegant solution for this?