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"]
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"]
There's also
stride
to generate a reversed index:It also works well with
map
:Note:
stride
starts its index at 1, not at 0, contrary to other Swift sequences.However, to anyone reading this in the future: use
.reverse()
instead to actually reverse an array, it's the intended way.Like this, maybe:
Oh, wait.. I have to use
for
loop, right? Then like this probably:Here is the most simpler way.