- 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ù)據(jù)可視化:從小白到數(shù)據(jù)工程師的成長(zhǎng)之路
- Python數(shù)據(jù)挖掘:入門、進(jìn)階與實(shí)用案例分析
- Google Visualization API Essentials
- 企業(yè)大數(shù)據(jù)系統(tǒng)構(gòu)建實(shí)戰(zhàn):技術(shù)、架構(gòu)、實(shí)施與應(yīng)用
- 大數(shù)據(jù)Hadoop 3.X分布式處理實(shí)戰(zhàn)
- gnuplot Cookbook
- Apache Kylin權(quán)威指南
- 數(shù)據(jù)庫(kù)應(yīng)用系統(tǒng)開發(fā)實(shí)例
- 大數(shù)據(jù)技術(shù)原理與應(yīng)用:概念、存儲(chǔ)、處理、分析與應(yīng)用
- Access數(shù)據(jù)庫(kù)開發(fā)從入門到精通
- Spring MVC Beginner’s Guide
- 數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用
- 大數(shù)據(jù)用戶行為畫像分析實(shí)操指南
- MySQL 8.0從入門到實(shí)戰(zhàn)