Ranges Type in Swift

Rashad Shirizada
4 min readJul 7, 2022
Source

In Swift, ranges are used to specify values that fall between a lower and upper bound. Ranges may be used to slice up arrays, determine whether a value is included inside a range, and much more.

Types of Ranges

Swift has a variety of range kinds that you might employ. The range operator is the most straightforward method of interacting with them. Let’s go over the various kinds that Swift offers.

So, there are 3 types of Ranges in Swift:

  1. Closed ranges with a…b
  2. Half-open ranges with a..
  3. One-sided ranges with a…, …b and ..

1. Closed Ranges

The simplest operator is the closed range operator a…b. It includes every item, including b, between the lower bound, frequently referred to as a, and the upper bound, frequently referred to as b. When we say “0 to 20, including 20,” we mean this.

Here’s an illustration:

let numbers = 0…9for num in numbers {
print(num)
}

The… operator is used in the code above to print all numbers in a for loop between 0 and 9. In the example above, the type of numbers is ClosedRange. This is a generic; we’ve made an integer-based value of type ClosedRange.

--

--