- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 136字
- 2021-07-02 23:54:25
for loops
Swift provides for and for-in loops. We can use the for-in loop to iterate over items in a collection, a sequence of numbers such as ranges, or characters in a string expression. The following example presents a for-in loop to iterate through all items in an Int array:
let scores = [65, 75, 92, 87, 68]
var teamScore = 0
for score in scores {
if score > 70 {
teamScore = teamScore + 3
} else {
teamScore = teamScore + 1
}
}
and over dictionaries:
for (cheese, wine) in cheeseWinePairs{
print("\(cheese): \(wine)")
}
As C styles for loops with incrementers/decrementers are removed from Swift 3.0, it is recommended to use for-in loops with ranges instead, as follows:
var count = 0
for i in 0...3 {
count + = i
}
推薦閱讀
- MySQL數據庫進階實戰
- Python數據挖掘:入門、進階與實用案例分析
- Visual Studio 2015 Cookbook(Second Edition)
- Libgdx Cross/platform Game Development Cookbook
- 文本挖掘:基于R語言的整潔工具
- UDK iOS Game Development Beginner's Guide
- Enterprise Integration with WSO2 ESB
- 軟件成本度量國家標準實施指南:理論、方法與實踐
- OracleDBA實戰攻略:運維管理、診斷優化、高可用與最佳實踐
- Python數據分析與挖掘實戰(第3版)
- Python數據分析從小白到專家
- 云原生架構:從技術演進到最佳實踐
- 大數據計算系統原理、技術與應用
- Redis 6開發與實戰
- R數據挖掘實戰