Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse array in Swift without using ".reverse()"?

Tags:

arrays

swift

I have array and need to reverse it without Array.reverse method, only with a for loop.

var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
like image 434
Milos Mandic Avatar asked Sep 05 '25 03:09

Milos Mandic


1 Answers

Swift 3:

var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]

var reversedNames : [String] = Array(names.reversed())

print(reversedNames)  // ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
like image 199
SpaceX Avatar answered Sep 08 '25 01:09

SpaceX