Sequence Type forEach block
suggest changeA 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
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents