Swift Ranges with examples


In this tutorial, you are going to learn about ranges in Swift and its types with the help of examples.

A range in Swift refers to a series of values between two numeric intervals which are known as lower bound and upper bound.

Imagine if the lower bound is 1 and the upper bound is 5, then the range will include all the numbers between these values including 1 and 5. So the range will contain 1, 2, 3, 4, 5.

Below is an example of range in Swift:

var numbers = 1...5

In the above example, ... is the operator for range. 1...5 includes the values 1, 2, 3, 4, 5. In this example, 1 is the lower bound interval which is the first value and 5 is the upper bound interval which is the last value.

We can use the for loop to print or access each value of the intervals like you can see below:

// 1...5 is the close range
for numbers in 1...5 {
  print(numbers)
}

Output:

1
2
3
4
5

Types of Swift ranges

Now it’s time to introduce you to the types of ranges available in Swift.

In Swift programming language, there are basically 3 types of ranges available that are listed below:

  • Closed range
  • Half-open range
  • One-sided range

Now let’s discuss each type of range with examples to understand it in detail:


Closed range

A closed range in Swift includes all the values between the lower bound and the upper bound in the interval and also includes the lower bound and upper bound values.

To declare a closed range, we use 3 dots operator ....

At the very beginning, we have already started with the example of a closed range.

Below is another example of a Swift closed range:

// 2...5 is the close range
for numbers in 1...3 {
  print(numbers)
}

Output:

2
3
4
5

In this example, we have created a closed range 2...5. At it is a closed range, so it will contain all the values even the lower and upper bound also. Here lower bound is 2 and the upper bound is 5.

We have used the for loop to access and print all the values from the range.

Half-open range

In a half-open range, it includes all the values from the lower bound to the upper bounds, but it excludes the upper bound value or the last number.

We use the ..< operator to denote a half-open range. Below is the example:

for numbers in 2..<5 {
  print(numbers)
}

Output:

2
3
4

In our above example, we have created a half-open range 1..<5 and used for loop to access the values in the range. In the output, we can see that the range excludes the upper bound value 5.


One-sided range

In Swift, a one-sided range includes numeric values that up to infinite in one direction. A one-sided range can be created either by using the ... or ..< operator.

In a one-sided range, we only set either the lower bound or the upper bound.

Below is an example of a one-sided range using the ... operator:

let myRange1 = 3...

This range includes all the elements from 3 to infinite like 3, 4, 5, 6, 7, 8 ….∞ . It means, the upper bound value is infinite.

Now see another example below:

let myRange2 = ..<3

It is another type of one-sided range with the operator ..<. It contains all the values from 3 to -∞ like 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7 … ∞ .

We can verify the range if it contains a specific number:

// one-sided range using the ..< operator
let myRange1 = ..<3

// checking if -100 is in our range
print(myRange1.contains(-100))

// one-sided range using the ... operator
let myRange2 = 3...

// checking if 154 is in our range
print(myRange2.contains(154))

Output:

true
true

In the above code, we have used the contains() method. This method is checking if a specific element is present in the range or not.


Accessing array elements using a range

Using Swift range, we can access elements of an array. Below is given an example of accessing array items using a Swift range:

let fruitsArray = ["Mango", "Apple", "Banana", "Strawberry", "Lychee"]

print(fruitsArray[0...4]) 

print(fruitsArray[0...2]) 

Output:

["Mango", "Apple", "Banana", "Strawberry", "Lychee"]
["Mango", "Apple", "Banana"]

Important notes about Swift ranges

Below is given some important notes on ranges in Swift:

  • The lower bound value of the range must be smaller than the upper bound value of the range.
  • Both the lower bound value and the upper bound value can be negative but should follow the first note.

In the example below, the ranges are invalid as the lower bound value is bigger than the upper bound value:

5...2  // Invalid

A valid range is given below where the lower bound value is smaller than the upper bound value:

2...5   // Valid

Below are given some ranges with both or any one of lower bound or upper bound value negative:

-5...2 // Valid

-5...-2  // Invalid

-2...-5 // Valid