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 reverse()
In this tutorial, we will learn about the Swift Array reverse() method with the help of examples.
The reverse() method returns the array in reverse order.
Example
// create an array of prime numbers
var prime = [2, 3, 5, 7]
// reverse the order of array elements
prime.reverse()
print(prime)
// Output: [7, 5, 3, 2]
reverse() Syntax
The syntax of the array reverse() method is:
array.reverse()
Here, array is an object of the Array class.
reverse() Parameters
The reverse() method doesn't take any parameters.
max() Return Values
The reverse() method doesn't return any value. It updates the existing array.
Example 1: Swift Array reverse()
// array of operating systems
var os = ["Windows", "macOS", "Linux"]
print("Original Array:", os)
// reverse array
os.reverse()
// updated array
print("Updated Array:", os)
Output
Original Array: ["Windows", "macOS", "Linux"] Updated Array: ["Linux", "macOS", "Windows"]
Here, we can see that the reverse() method reverses the order of array elements.
Did you find this article helpful?