Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A for-in loop is a control flow statement that iterates over any type conforming to the Sequence protocol. Under the hood, it abstracts the creation of an Iterator (via the makeIterator() method) and repeatedly calls its next() method until nil is returned, executing the loop body for each unwrapped element.
for element in sequence {
    // Statements to execute
}

Core Mechanics

  • Implicit Declaration: The iteration variable (element) is implicitly declared as a constant (let) that is scoped exclusively to the loop’s body. It is initialized with the current sequence value at the start of each iteration.
  • Mutable Iteration Variables: While the iteration variable is a constant by default, Swift natively supports mutable iteration variables directly in the loop signature. By explicitly declaring the variable with var, the loop allocates a mutable local copy of the element for that specific iteration.
for var element in sequence {
    element.mutate() // Mutates the local copy, not the underlying sequence element
}
  • Sequence Conformance: The right-hand side of the in keyword must evaluate to a type that conforms to Sequence. Common standard library types include Array, Dictionary, Set, String, and Range.

Syntax Variations

Pattern Matching (for case) The for-in loop integrates directly with Swift’s pattern matching engine. By utilizing the for case syntax, the loop evaluates each element against a pattern, executing the body only for matching elements and silently skipping the rest. This is highly idiomatic for extracting associated values from enumerations or unwrapping non-nil optionals.
// Iterating over specific enum cases and binding associated values
for case let .success(value) in results {
    // Executes only for .success cases
}

// Iterating over non-nil optionals (the `?` pattern)
for case let item? in sequenceOfOptionals {
    // Executes only for non-nil elements, binding the unwrapped value to 'item'
}
Range Iteration Iterating over a ClosedRange (...) or Range (..<).
for index in 1...5 {
    // Iterates 1, 2, 3, 4, 5
}

for index in 1..<5 {
    // Iterates 1, 2, 3, 4
}
The Wildcard Pattern If the current value of the sequence is not required within the loop body, the wildcard pattern (_) is used to bypass variable allocation and suppress compiler warnings.
for _ in 1...10 {
    // Executes 10 times; the iteration value is discarded
}
Filtering with where Clauses A where clause can be appended to the loop signature to evaluate a boolean condition before executing the loop body. The body is only executed if the condition evaluates to true. This acts as an inline filter, bypassing the need for a nested if statement.
for number in 1...10 where number % 2 == 0 {
    // Executes only for 2, 4, 6, 8, 10
}
Tuple Destructuring When iterating over collections whose elements are tuples (such as a Dictionary, which is a sequence of (Key, Value) pairs), the for-in loop supports inline tuple destructuring.
for (key, value) in dictionary {
    // 'key' and 'value' are implicitly declared constants
}
Striding For non-standard step increments, the for-in loop consumes the sequence generated by the global stride(from:to:by:) (exclusive) or stride(from:through:by:) (inclusive) functions.
for value in stride(from: 0, to: 10, by: 2) {
    // Iterates 0, 2, 4, 6, 8
}
Master Swift with Deep Grasping Methodology!Learn More