Swift Language Sequence Type forEach block


Example

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
    print(item)
}

*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 {
        return
    }
}