[Solved] Swift `for-in` loop Range and ClosedRange errors


Swift ranges are written as 0...10, not [0...10].


[0...10] creates a single-item array. The first item in the array is a 0...10 range.

Using for i in [0...10] thus iterates over that single-item array, not the range itself. The iterated value i will be of type Range or ClosedRange.

To iterate over each Int in the range, as you expect to be doing, use the range 0...10 without the brackets:

for i in 0...10 {
    // ...
}

for i in 0..<10 {
    // ...
}

https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

You can also use for-in loops with numeric ranges. This example prints the first few entries in a five-times table:

for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

The sequence being iterated over is a range of numbers from 1 to 5, inclusive, as indicated by the use of the closed range operator (...).

solved Swift `for-in` loop Range and ClosedRange errors