seq.forEach()

  • โ›” break : not allowed.

  • โ›” continue : not allowed.

  • โ— return : exit only from the current call to body. ๐Ÿ‘‰ 1๏ธ

// `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