seq.forEach()

// `seq.forEach(_:)` declaration:
func forEach(
    _ body: (Self.Element) throws -> Void
) rethrows
// sequence.forEach()
(1...10).forEach { n in 
    print(n)
    // ⭐️ return from this closure, not from the `forEach` method
    if n > 2 { return } 
}
// 1, 2, ..., 10

forEach vs. for-in loop

Last updated

Was this helpful?