Swift Array reverse()

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.