Swift, how to get a random item from an array

This tutorial belongs to the Swift series

Suppose you have an array in Swift, like this:
let items = [1, 2, 3]
and you want to get a random number out of it.

The Array data type provides the randomElement() function that returns an Element…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This tutorial belongs to the Swift series

Suppose you have an array in Swift, like this:

let items = [1, 2, 3]

and you want to get a random number out of it.

The Array data type provides the randomElement() function that returns an Element?:

let item = items.randomElement()


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-15T05:00:00+00:00) Swift, how to get a random item from an array. Retrieved from https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/

MLA
" » Swift, how to get a random item from an array." flaviocopes.com | Sciencx - Tuesday June 15, 2021, https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/
HARVARD
flaviocopes.com | Sciencx Tuesday June 15, 2021 » Swift, how to get a random item from an array., viewed ,<https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift, how to get a random item from an array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/
CHICAGO
" » Swift, how to get a random item from an array." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/
IEEE
" » Swift, how to get a random item from an array." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/. [Accessed: ]
rf:citation
» Swift, how to get a random item from an array | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/15/swift-how-to-get-a-random-item-from-an-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.