Sequence Type forEach block

suggest change

A type that conforms to Sequence (SequenceType in Swift 2) can walk its elements in a closure:

collection.forEach { print($0) }

The same could also be done with a named parameter:

collection.forEach { item in
    print(item)
}

break and continue are not allowed in the closure. return leaves only the current invocation, which is similar to continue. The next element is still visited.

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

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents