Swift 進(jìn)階之 For 循環(huán)
將當(dāng)前代碼貼到Playground 中運(yùn)行即可看到效果
//: Playground - noun: a place where people can play
import UIKit
// 正序輸出 1 2 3 4 5 包含1 5
for i in 1...5 {
print(i)
}
// 僅循環(huán)忽略變量索引值
for _ in 1...5 {
}
// 正序輸出 1 2 3 4 不包含5
for i in 1..<5 {
print(i)
}
// 逆序輸出 10 9 8 7 6 5 4 3 2 1 0
for i in (0...10).reversed() {
print(i)
}
for i in stride(from: 10, through: 0, by: -1) {
print(i)
}
// 逆序輸出 10 9 8 7 6 5 4 3 2 1 不包含0
for i in stride(from: 10, to: 0, by: -1) {
print(i)
}
// 遍歷數(shù)組
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)")
}
// 遍歷字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}