For loop based on array length in Swift
In Swift 3 and Swift 4 you can do:
for (index, name) in names.enumerated()
{
...
}
You need to specify the range. If you want to include nameArrayLength
:
for index in 1...nameArrayLength {
}
If you want to stop 1 before nameArrayLength
:
for index in 1..<nameArrayLength {
}
for i in 0..< names.count {
//YOUR LOGIC....
}
for name in 0..< names.count {
//YOUR LOGIC....
print(name)
}
for (index, name) in names.enumerated()
{
//YOUR LOGIC....
print(name)
print(index)//0, 1, 2, 3 ...
}