Swift Language Loops Sequence Type forEach block

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


A type that conforms to the SequenceType protocol can iterate through it's elements within a closure:

collection.forEach { print($0) }

The same could also be done with a named parameter:

collection.forEach { item in

*Note: Control flow statements (such as break or continue) may not be used in these blocks. A return can be called, and if called, will immediately return the block for the current iteration (much like a continue would). The next iteration will then execute.

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

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

Got any Swift Language Question?