Select Page
Programming Languages - Swift tutorials - Tech tutorial

# Swift Ranges

With Swift, ranges are an excellent tool that can be used to work with arrays to perform complex operations with ease. Ranges can help reduce and simplify the work and code involved. In this post, we will explain what ranges are in Swift, the types of ranges, and how to use them with relevant examples. Let’s begin!

## What are Swift Ranges?

A Swift range is a range operator. In a range, we define two values which can be compared against one another, such as integers or characters. Ranges are an excellent way to set up an array with ease.

## How do I write a Swift range?

Writing a Swift range is easy! We begin with the lower bound value, insert “…”, and end with an upper bound value. The “…” is an operator that includes the upper and lower limits of the range.

Let’s look at a range of integers:

```//Creating a range from 1 to 10
let basicRange10 = 1...10
for i in basicRange10 {
print(i)
}
```

Output:

``` 1 2 3 4 5 6 7 8 9 10
```

Notice how we use the “…” operator to define a range of integers from 1 to 10. In this example, we iterated the range over a for loop and printed each integer value.

What if we wanted to iterate over a range of array items?

```//Printing only array items in the range
print(groceryList[2...4])
```

Output:

```["bread", "milk", "peanut butter"]
```

Even though the array index begins at 0, the range begins at 1. This is the reason why the output was not [“bananas”, “bread”, “milk”]

## Types of Swift Ranges

Now that we have a basic understanding of Swift ranges, let’s look at the different types. There are 3 types of Swift ranges:

• Closed
• Half open
• One-sided

## Closed

A closed range operator (a. . .b) is a range that includes your two lower and upper end values.  We use the “…” operator as seen in the previous two examples.

Closed range operator example:

```//Print even numbers
let twentyToThirty = 20...30
for i in twentyToThirty {
if( i % 2 == 0){
print(i)
}
}
```

Output:

```20
22
24
26
28
30
```

Our lower and upper values are printed out as expected when using a closed range operator.

## Half Open

A half open range operator (a. . <b) is a range that includes the lower range value but excludes the upper range value. This is useful in arrays where you are counting up to the length of the array but not including the length of the array.

Let’s look at an example where someone scored the end value of a half open range:

```//What if someone scored the upper range value of a half open range?
let passingRange = 0..<60
if( passingRange.contains(60)){
print("60 is a passing score")
}else {
print("60 is not a passing score")
}
```

Output:

```60 is not a passing score
```

Thus, if someone scored a 60, it would not be considered a passing score if the test used a half open range instead of a closed range.

## One-sided

Lastly, we have the one-sided Swift range operator which can use “. . .” or the “. . <” operator with no lower or upper bound.

Let’s go back to our closed range example and make it one-sided:

```//What would happen if we had no upper bound?
let twentyToDotDotDot = 20...
for i in twentyToDotDotDot {
if( i % 2 == 0){
print(i)
}
}
```

We would see the wheel of death in our Playground app because we would try to find all the even numbers from 20 to infinity.

## Final thoughts

And, there you have it! An introduction to Swift ranges. But we are only scratching the surface of the true power and versatility of Swift ranges. Interested in learning more? Explore concepts such as converting, inspecting, clamping, and checking for containment of ranges at the Apple developer page, or become an iOS Developer with Udacity’s Nanodegree program. Happy coding!

## Glossary

• Swift Range: a range operator that defines values between a lower and upper value
• Closed range: a Swift range that includes your two lower and upper end values
• Half open range: a Swift range that only includes your lower end value
• One-sided range: a Swift range that has no lower or upper bound
Melissa Hong