- 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
}
推薦閱讀
- 數(shù)據(jù)挖掘原理與實(shí)踐
- 輕松學(xué)大數(shù)據(jù)挖掘:算法、場(chǎng)景與數(shù)據(jù)產(chǎn)品
- 從0到1:數(shù)據(jù)分析師養(yǎng)成寶典
- Game Development with Swift
- 大數(shù)據(jù)可視化
- 區(qū)塊鏈通俗讀本
- 數(shù)據(jù)庫應(yīng)用基礎(chǔ)教程(Visual FoxPro 9.0)
- 深入淺出數(shù)字孿生
- 數(shù)據(jù)驅(qū)動(dòng):從方法到實(shí)踐
- SQL優(yōu)化最佳實(shí)踐:構(gòu)建高效率Oracle數(shù)據(jù)庫的方法與技巧
- 信息學(xué)競(jìng)賽寶典:數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)
- PostgreSQL指南:內(nèi)幕探索
- Oracle RAC日記
- Python數(shù)據(jù)分析與數(shù)據(jù)化運(yùn)營(yíng)
- 菜鳥學(xué)SPSS數(shù)據(jù)分析