依据不同情形, swift提供很多种方式对数组进行迭代
比如有如下数组:
var heros = [
"Atani J", "Tirion Foordring", "Salou Farr", "Thrall", "Tassa Dahl"
]
func printHeros() {
for (var i = 0; i < heros.count; i++) {
println(heros[i])
}
}
printHeros()
func printHeros() {
for i in 0..<heros.count {
println(heros[i])
}
}
printHeros()
func printHeros() {
for (index, hero) in enumerate(heros) {
println("\(index): \(hero)")
}
}
printHeros()