Swift version: 5.1
Swift has built-in methods for shuffling arrays:
shuffled(), depending on whether you want the shuffling to happen in place or not.
var albums = ["Red", "1989", "Reputation"] // shuffle in place albums.shuffle() // get a shuffled array back let shuffled = albums.shuffled()
SAVE 20% ON iOS CONF SG The largest iOS conference in Southeast Asia is back in Singapore for the 5th time in January 2020, now with two days of workshops plus two days of talks on SwiftUI, Combine, GraphQL, and more! Save a massive 20% on your tickets by clicking on this link.
Available from iOS 8.0 – see Hacking with Swift tutorial 2
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.