English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Swift's Range, type, and use cases.
Range (interval) is a value interval. A simple range example is 0,1,2,3,4,5,6,7,8,9because numbers range from 0 to9is continuous.
We can quickly create an interval using the two range operators described below:
It includes all values within the interval (lowerbound to upperBound). It is declared using … (3a point) operator declaration.
For example: 1...3 defines an interval containing the value1,2,3range
It includes all values within the interval (lowerbound to upperBound), but does not include the last number (upperBound). It is declared using the ..< operator.
For example: 1..<3 Define the range containing values1and2range
The range created using the closed interval operator is called a closed interval. It includes all values from the lower bound to the upper bound.
//1... 3Define the range containing values1,2and3range for value in 1...3 { print(value) }
When running the program, the output is:
1 2 3
The above example created a range that includes1to3(1...3) of numbers. We used a for-in loop to view which values are included in the range. To learn more about for-in loop for more information, please visitSwift for-in loop) in loop
using for-in loop, we can see that the closed interval includes all values within the given range, including the lower limit(1) and the upper limit(3)
The interval created using the half-open interval operator is called a half-open interval. It includes all values from the lower bound to the upper bound, butdoes not includeupper limit.
// 1..<3 Define a range containing values 1,2 for value in 1..<3 { print(value) }
When running the program, the output is:
1 2
In the above example, we used for-in loop to view the working principle of the half-open interval.
Instead of printing all values, we can clearly see that using the half-open interval operator only prints1and2, and does not include the upper limit (i.e.3).
A unilateral interval is an interval that is as continuous as possible in one direction. It can be created using the half-open range interval operator and the closed interval operator, but the operator can only have a value on one side.
let range = ..<2 print(range.contains(-1)) print(range.contains(2))
When running the program, the output is:
true false
The above example uses the half-open interval operator to create a unilateral interval that includes values less than2of any number.
To verify our results, we used the contains method. If the element is within the interval, the contains method returns true, otherwise it returns false.
range.contains(-1) checks -1 whether it is within the interval. Since the upper limit of its unilateral interval is2 and-1 <2, so it is within the interval, and the statement print(range.contains(-1)) is output as true in the screen.
However, since the half-open interval operator is used, the upper limit (2) is not included in the interval. Therefore, range.contains(2) returns false.
let range = 2... print(range.contains(100)) print(range.contains(1))
When running the program, the output is:
true false
The above example uses closed range operator to create a one-sided range, which includes the range from2to greater than2of any value's number.
range.contains(10(0) check100 is within the range. Since it is a one-sided range, and100 is greater than2, so it is within the range and prints output true on the screen.
But, it has a lower bound value(2) therefore range.contains(1) returns false.
The starting point of the range must be less than or equal to its end point. In our example (lowerBound...upperBound), the lower bound value must be less than the upper bound value. However, it can be negative.
For example:
3...1 // Error: Cannot use upperBound < lowerBound to form Range -3...1 // This is a valid range. Lowerbound/Upperbound can include negative values.
We can use for-in loops to iterate over the entire range (excluding the side of the range).
We can also use range operators to accessOf the arrayElements.