How to Do a Swift For-In Loop With a Step

How can I do a Swift for-in loop with a step?

The Swift synonym for a "step" is "stride" - the Strideable protocol in fact, implemented by many common numerical types.

The equivalent of (i = 1; i < max; i+=2) is:

for i in stride(from: 1, to: max, by: 2) {
// Do something
}

Alternatively, to get the equivalent of i<=max, use the through variant:

for i in stride(from: 1, through: max, by: 2) {
// Do something
}

Note that stride returns a StrideTo/StrideThrough, which conforms to Sequence, so anything you can do with a sequence, you can do with the result of a call to stride (ie map, forEach, filter, etc). For example:

stride(from: 1, to: max, by: 2).forEach { i in
// Do something
}

How do a i+=2 for-loop in Swift?

Check this

for index in stride(from: 0, to: 5, by: 2){
print(index)
}

Swift iterate array with multiplier step

In Swift you can do this with help of stride.

let n = 3

for index in stride(from: 0, through: 100, by: n) {
print(index)
}

Output of the indexes:

0
3
6
9
12
15
18
21
24
27
30
33
36
39
42
...

For-loop where the step-value is greater than one

You can achieve this in Swift using the stride(from:to:by:) function like this:

for i in stride(from: 0, to: 9, by: 2) {
print(i)
}

Or using a forEach closure:

stride(from: 0, to: 9, by: 2).forEach {
print($0)
}

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 (...).

Can you tell a for-loop to conditionally advance by more than one step?

No, you can't change how a for-in loop iterates from within the loop.

A while loop with your own index counter is probably the simplest solution in this case. Though you may be able to make use of sequence(first:next:) in a for-in loop.

How to iterate for loop in reverse order in swift?

Xcode 6 beta 4 added two functions to iterate on ranges with a step other than one:
stride(from: to: by:), which is used with exclusive ranges and stride(from: through: by:), which is used with inclusive ranges.

To iterate on a range in reverse order, they can be used as below:

for index in stride(from: 5, to: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2

for index in stride(from: 5, through: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2, 1

Note that neither of those is a Range member function. They are global functions that return either a StrideTo or a StrideThrough struct, which are defined differently from the Range struct.

A previous version of this answer used the by() member function of the Range struct, which was removed in beta 4. If you want to see how that worked, check the edit history.

Mutability of the Iterator Element in a For-In loop with an Array in Swift

Just change it to var instead of let where you declare point. let is a constant.



Related Topics



Leave a reply



Submit