Swift - for-in 迴圈



for-in 迴圈迭代遍歷專案集合,例如數字範圍、陣列中的專案或字串中的字元。

語法

Swift 4 程式語言中for-in 迴圈的語法如下:

for index in var {
   statement(s)
}
For-In Loop

示例

var someInts:[Int] = [10, 20, 30]

for index in someInts {
   print( "Value of index is \(index)")
}

執行上述程式碼後,將產生以下結果:

Value of index is 10
Value of index is 20
Value of index is 30
swift_loops.htm
廣告