Reverse an Array in Swift

In this Swift programming tutorial, I am going to let you know how you can reverse a Swift array with examples.

Reverse an array
Reverse an array

Reversing an array using the Swift reverse() method

Using the reverse() method is the easiest and simple way to reverse an array in Swift. It is a built-in method.

Let’s see an example:

var fruitArray = ["Banana", "Apple", "Mango"]


print( fruitArray )


["Mango", "Apple", "Banana"]

Using reversed() method to reverse a string

In the above example, our original array has been changed. But if we want to keep the original array unchanged and create a new array with the element reversed then we can use the reversed() method instead of reverse() method.

Both the reverse() and reversed() methods work as same. But the reverse() method changes the original string whereas the reversed() method keeps the original string unchanged and creates a new array.

Using reversed() method is useful in a situation where you have to reverse an immutable array.

Below is how we can use the reversed() method to reverse an array:

let fruitArray = ["Banana", "Apple", "Mango"]

var reversedFruit = fruitArray.reversed()

var reversedFruitArray = Array(reversedFruit)

print( reversedFruitArray )


["Mango", "Apple", "Banana"]

In the above example, we have created a new array instead of changing the original array.