A half-open interval from a lower bound up to, but not including, an upper bound.
SDK
- Xcode 6.0.1+
Framework
- Swift Standard Library
Declaration
Overview
You create a Range
instance by using the half-open range operator (..<
).
You can use a Range
instance to quickly check if a value is contained in a particular range of values. For example:
Range
instances can represent an empty interval, unlike Closed
.
Using a Range as a Collection of Consecutive Values
When a range uses integers as its lower and upper bounds, or any other type that conforms to the Strideable
protocol with an integer stride, you can use that range in a for
-in
loop or with any sequence or collection method. The elements of the range are the consecutive values from its lower bound up to, but not including, its upper bound.
Because floating-point types such as Float
and Double
are their own Stride
types, they cannot be used as the bounds of a countable range. If you need to iterate over consecutive floating-point values, see the stride(from:
function.