Swift Array shuffle()

The shuffle() method shuffles all the elements of an array.


var numbers = [1, 2, 3, 4, 5]

// shuffle elements of numbers numbers.shuffle()
print(numbers) // Output: [ 5, 1, 2, 4, 3 ]

shuffle() Syntax

The syntax of the array shuffle() method is:


Here, array is an object of the Array class.

shuffle() Parameters

The shuffle() method doesn't take any parameters.

shuffle() Return Value

The shuffle() method doesn't return any value. It only shuffles the element of the current array.

Example: Swift Array shuffle()

var languages = ["Swift", "C", "Java"]

// shuffle elements of languages languages.shuffle()
print(languages) var priceList = [12, 21, 35]
// shuffle elements of priceList priceList.shuffle()


["Swift", "Java", "C"]
[21, 35, 12]

Here, we have used the shuffle() method to shuffle the elements of the languages and priceList arrays respectively.

Did you find this article helpful?