๐ฐfor...in vs. forEach
Swift โฉ flow control โฉ loop โฉ for...in vs. .forEach()
flow control
for ... in
.forEach()
continue
โ skip to the next iteration.
โ not allowed
break
โ break the iteration completely.
โ not allowed
๐ replit
โ error: return invalid outside of a func.
for n in 1...10 {
if n == 3 { return } // โ error
// ^^^^^^
// โ error: return invalid outside of a func
}
๐ replit
(1...10).forEach { n in
if n > 2 { continue } // โญ `continue` is inside a closureโ
// ^^^^^^^^
// โ error: 'continue' is only allowed inside a loop
}
๐ replit
(1...10).forEach { n in
if n > 2 { break } // โญ `break` is inside a "closure"
// ^^^^^
// โ error:
// โข unlabeled 'break' is only allowed inside a loop or switch
// โข labeled 'break' is required to exit an `if` or `do`
}
Last updated
Was this helpful?