by Sai gowtham

How to reverse an array in Swift

In this tutorial, we are going to learn about how to reverse an array in Swift

Using the reverse() method

We can reverse an array in Swift by using the built-in `reverse() method.

Here is an example, that reverses the following array:

var arr = ["a", "b", "c"]

arr.reverse()

print(arr)

Output:

["c", "b", "a"]

Similarly, you can also reverse the array in swift like this:

var arr = ["a", "b", "c"]

var reversedArr : [String] = []

for el in arr.reversed(){
    reversedArr.append(el)
}

print(reversedArr)

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course