依据不同情形,swift提供很多种方式对数组进行迭代
比如有如下数组:
var heros = [ "atani J","Tirion Foordring","Salou Farr","Thrall","Tassa Dahl" ]
1. 通过变量递增的for循环
func printHeros() { for (var i = 0; i < heros.count; i++) { println(heros[i]) } } printHeros()
2. 使用Range操作符
func printHeros() { for i in 0..<heros.count { println(heros[i]) } } printHeros()
3. 使用enumerate进行枚举
func printHeros() { for (index,hero) in enumerate(heros) { println("\(index): \(hero)") } } printHeros()原文链接:https://www.f2er.com/swift/327081.html