For loops in Swift – For-In, For-Case-In, Where, Stride And Sequence

Initially Swift contained C-style for loops just like
Objective-C, but because of a lack of usage C-style for loops were removed as
alternatively you can use the For-In, For-Case-Where-In,  Stride and Sequence keywords to accomplish the
same tasks.

For-In

The first example is the for-in loop which makes it very easy to iterate through data structures.  In this example we’ll go through an array:

// this example iterates through an array printing 1 2 3
let values = [1,2,3]
for x in values {
    print(x)
}

You can also easily go through number ranges using for-in as
below:

// these two examples iterate through a number range printing 1 2 3
for x in 13 {
    print(x)
}
for x in 1..<4 {
    print(x)
}

Going through dictionaries Is easy using for (key, value)
in:

// this example shows how to iterate through a dictionary
let keyValues = ["one": 1, "two": 2, "three": 3]
for (key, value) in keyValues {
    print("Key: \(key), Value: \(value)")
}

Where

Filtering can be adding using for-case-in and where like in this example where we only print odd numbers – notice you must add the let keyword:

// this example prints 1 3
// filters out the 2 which is an even number
for case let x in 13 where x % 2 > 0 {
    print(x)
}

You can omit the where statement when filtering out nil
values using optionals .some case like this:

// we can use some optionals to shorten this down using .some
// this example prints 1 2 3
let valuesWithNil = [1, nil, 2, nil, 3]
for case let .some(x) in valuesWithNil {
    print(x)
}

Now, what if you don’t want to go through each value – this is
where Stride and Sequence come in.

Stride

With stride you can iterate by a specific number like in these cases below where we will increase the value by 2 at each iteration – stride(from: in:) excludes the final value, and stride(from: in:) includes the final value:

// this example prints 1 3 5
// this is equivalent to c for loop statement
// for(x = 1; x < 7; x+=2)
for x in stride(from: 1, to: 7, by: 2) {
    print(x)
}

// this example prints 1 3 5 7
// for(x = 1; x <= 7; x+=2)
for x in stride(from: 1, through: 7, by: 2) {
    print(x)
}

Stride only allows you to go through the sequence using a
constant value, and this is where sequence comes in which we can use for more
complex cases. 

Sequence

Sequence allows usage of the ternary operator within a closure so we can iterate using some dynamic interval like in this example:

// This example doubles the x value at each iteration
// and prints 1 2 4 8
for x in sequence(first: 1, next: {$0 * 2 < 10 ? $0 * 2 : nil}) {
    print(x)
}

The $0 represents the first value passed to a closure and in this case represents the current value.

You can also combine sequence and stride with where for further filtering:

// this example shows how to combine a for-case-in statement
// with a sequence, this prints 1 2 8
for case let x in sequence(first: 1, next: {$0 * 2 < 10 ? $0 * 2 : nil}) where x != 4 {
    print(x)
}

As you can see using For-In, For-Case-In, Where, Stride And
Sequence allows us to create very complex for-loops without the need for the
standard C for loop.

Original article: For loops in Swift – For-In, For-Case-In, Where, Stride And Sequence

©2019 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *