I have array and need to reverse it without Array.reverse
method, only with a for
loop.
var names:[String] = [\"Apple\", \"Microsof
do you mean
var names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var newNames = [String]()
for var i = names.count - 1; i >= 0 ; i-- {
newNames.append(names[i])
}
names = newNames
or
names.map {newNames.insert($0, atIndex: 0)}
names = newNames
var names:[String] = [ "A", "B", "C", "D", "E","F","G"]
var c = names.count - 1
var i = 0
while i < c {
swap(&names[i++],&names[c--])
}
Here is how I did it and there is no warning for Swift 3
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for name in names.enumerate() {
let newIndex = names.count - 1 - name.index
reversedNames.append(names[newIndex])
}
or just simply
reversedNames = names.reverse()