Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Array shuffle()
In this tutorial, we will learn about the Swift array shuffle() method with the help of examples.
The shuffle() method shuffles all the elements of an array.
Example
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:
array.shuffle()
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()
print(priceList)
Output
["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?