每個塊的序列型別

符合 SequenceType 協議的型別可以遍歷閉包中的元素:

collection.forEach { print($0) }

使用命名引數也可以這樣做:

collection.forEach { item in
    print(item)
}

*注意:這些塊中可能不使用控制流語句(例如中斷或繼續)。可以呼叫 return,如果呼叫,將立即返回當前迭代的塊(非常類似於 continue)。然後執行下一次迭代。

let arr = [1,2,3,4]

arr.forEach {
    
    // blocks for 3 and 4 will still be called
    if $0 == 2 {
        return
    }
}