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
Was this helpful?