Swift Range

A range is an interval of values.

Range in Swift

A range is an interval of values.

We use ranges in Swift to define values between a lower and upper limit. Ranges are useful for creating slices of arrays, checking if a value is contained in a range, and much more.

We can create range in swift using two range operators described below:

1. Closed Range Operator (lowerBound...upperBound)

It includes all the values in the interval(lowerbound to upperBound). It is declared using (3 dots)operator.

E.g: 1...3 Defines range containing values 1,2,3

2. Half Open Range Operator (lowerBound..<upperBound)

It includes all the values in the interval(lowerbound to upperBound) but excludes the last (upperBound) number. It is declared using ..< operator.

E.g: 1..<3 Defines range containing values 1 and 2

Types of Ranges in Swift

1. Closed Range (lowerBound...upperBound)

Ranges created using the closed range operator are called as closed range. It includes all the values from lowerbound to upperbound.

Printing closed range values using for-in loop:

/// 1...3 Defines a range containing values 1, 2 and 3
for value in 1...3 {
    print(value)
}

The above example creates a range that contains numbers from 1 to 3 ( 1...3 ). We used a for-in loop to see what values the range contains. Using for-in loop, we can see closed range contains all values in the given range including the lower (1) and upper (3) bound values.

2. Half Open Range (lowerBound..<upperBound)

Ranges created using the half open range operator are called as half open ranges. It includes all values from lowerbound to upper bound but excludes the upper bound value.

Printing half open range values using for-in loop:

/// 1..<3 Defines a range containing values 1,2
for value in 1..<3 {
    print(value)
}

In the above example, we’ve used for-in loop to see how half-open range works. Instead of printing all the values, we can clearly see using half open operator only prints 1 and 2, and it excludes the upper bound value (i.e. 3).

3. One sided range

One sided range are those types of range that continue as far as possible in one direction. It can be created using both half open range operator and closed range operator but the operator can have a value on only one side.

One-sided range less than 2:

let range =..<2
print(range.contains(-1))
print(range.contains(2))

The above example creates a one sided range using half-open range operator that contains any numbers less than two.

To validate our result we have used contains method. The contains method simply returns true if the element lies inside the range otherwise it returns false .

range.contains(-1) checks if -1 lies inside the range or not. Since, its one sided range with upper bound 2 ,and -1 < 2 it lies inside the range and range.contains(-1) returns true. However, because of half-open range operator, the upper bound value(2) does not contains inside the range. So, range.contains(2) returns false.

One-sided range starting from 2:

let range = 2...
print(range.contains(100))
print(range.contains(1))

The above example creates a one sided range using closed operator that contains numbers from 2 to any values greater than 2.

range.contains(100) checks if 100 lies inside the range or not. Since, its one sided range and 100 is greater than 2, it lies inside the range so returns true. However, it has a lower bound value (2), so range.contains(1) returns false.

An example of using range operators with Array in Swift

let tags = ["swift", "range-operators", "range", "closed-range", "countable-range", "countable-closed-range", "ios-development"]

/// ["swift", "range-operators", "range"]
print(tags[0...2])

/// ["swift", "range-operators"]
print(tags[0..<2])

/// ["range-operators", "range", "closed-range", "countable-range", "countable-closed-range", "ios-development"]
print(tags[1...])

/// ["swift", "range-operators", "range", "closed-range"]
print(tags[...3])

/// ["range-operators", "range"]
let tagR: Range = 1..<3
print(tags[tagR])

/// ["range-operators", "range", "closed-range"]
let tagCR: ClosedRange = 1...3
print(tags[tagCR])

/// range-operators
/// range
let tagsCR: CountableRange = 1..<3
for index in tagsCR {
    print(tags[index])
}

/// ["range-operators", "range", "closed-range"]
let tagCCR: CountableClosedRange = 1...3
print(tags[tagCCR])

Conclusion

Range is Swift variant of NSRange which we know from Objective-C although its not exactly the same, which allow us to select parts of Strings, collections, and other types.

Few things to remember about Range in Swift:

  • A range's start must be less than or equal to its end. i.e; the lower bound value must be smaller than upper bound value. However, it can be a negative value.
  • We can iterate over range (except one sided range) using for-in loops.
  • We can also use range operator to access elements of an array.