How to pick a random element from an array (Swift)
In this tutorial, you will learn how to pick a random element from an array using Swift.
You will Need Mac, the latest Xcode to complete this tutorial.
This story was originally published on AppMakers.Dev
Open Xcode and create a new playground titled “How to pick a random element from an array using Swift”
Create an array and run the randomElement() method on it.
let teslaCars = ["Model S", "Model X", "Model 3", "Cybertruck"]print(teslaCars.randomElement() ?? "Nothing")
Run and see the results
Keep in mind that at the moment of writing this post we used Xcode 11 and Swift 5.
If anything will change in the future, it is OK, just use the new API calls for the functions mentioned in this post.
Thanks for reading.
If you love this post, feel free to clap 👏👏 and share it.
🏷 COURSE DISCOUNT: Learn more about Table Views in the NEW UITableView Swift 5 Course
Related Posts:
Best iOS Development Podcasts for App Developers
Visit AppMakers.Dev Website for interesting posts about App Development. Follow the Editor