When to use forEach(_:) instead of for in? When to use forEach(_:) instead of for in? swift swift

When to use forEach(_:) instead of for in?


There is no performance benefit offered by forEach. In fact, if you look at the source code, the forEach function actually simply performing for-in. For release builds, the performance overhead of this function over simply using for-in yourself is immaterial, though for debug builds, it results in an observable performance impact.

The main advantage of forEach is realized when you are doing functional programming, you can add it to a chain of functional calls, without having to save the prior result into a separate variable that you'd need if you used for-in syntax. So, instead of:

let objects = array.map { ... }    .filter { ... }for object in objects {    ...}

You can instead stay within functional programming patterns:

array.map { ... }    .filter { ... }    .forEach { ... }

The result is functional code that is more concise with less syntactic noise.

FWIW, the documentation for Array, Dictionary, and Sequence all remind us of the limitations introduced by forEach, namely:

  1. You cannot use a break or continue statement to exit the current call of the body closure or skip subsequent calls.

  2. Using the return statement in the body closure will exit only from the current call to body, not from any outer scope, and won't skip subsequent calls.


I recently ran across a use case where using forEachwas preferable in a tangible way to for in. Let's say you want to remove all sublayers from a layer. A statement such as the below doesn't work as you need to unwrap the [CALayer]

for layer in self.videoContainerView.layer.sublayers!

If sublayers are nil, you will get a crash. This forces you to check to see if there are sublayers first. However, a forEach makes this much simpler as in the following:

self.videoContainerView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }


They are more or less interchangeable, but there are two important differences.

  1. break/continue only work in a for .. in
  2. return in forEach will exit the closure, but will not halt the iteration.

The reason for this is that for .. in is a special form in the language (which allows break and continue to work as you expect). It is something that you can't implement in an identical way using the language itself.

However, forEach is not a special form and can be re-implemented identically by writing it as a function.

extension Sequence {    func myOwnForEach(_ body: (Self.Element) throws -> Void) rethrows {        let it = Self.makeIterator()        while let item = it.next() {            body(item)        }    }}