Instance Method

forEach(_:)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

Declaration

func forEach(_ body: (UInt8) throws -> Void) rethrows

Parameters

body

A closure that takes an element of the sequence as a parameter.

Discussion

The two loops in the following example produce the same output:

let numberWords = ["one", "two", "three"]
for word in numberWords {
    print(word)
}
// Prints "one"
// Prints "two"
// Prints "three"

numberWords.forEach { word in
    print(word)
}
// Same as above

Using the forEach method is distinct from a for-in loop in two important ways:

  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.

See Also

Iterating Over the Bytes of a Digest

func makeIterator() -> Array<UInt8>.Iterator

Generates an iterator over the bytes of the digest.

typealias SHA256Digest.Iterator

A type for the iterator over the digest.

typealias SHA256Digest.Element

A type for the elements of the digest.

func enumerated() -> EnumeratedSequence<SHA256Digest>

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.

var underestimatedCount: Int

A value less than or equal to the number of elements in the sequence, calculated nondestructively.