An interval from a lower bound up to, and including, an upper bound.
- Xcode 8.0+
- Swift Standard Library
You create a
Closed instance by using the closed range operator (
Closed instance contains both its lower bound and its upper bound.
Because a closed range includes its upper bound, a closed range whose lower bound is equal to the upper bound contains that value. Therefore, a
Closed instance cannot represent an empty range.
Using a Closed Range as a Collection of Consecutive Values
When a closed 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
in loop or with any sequence or collection method. The elements of the range are the consecutive values from its lower bound up to, and including, its upper bound.
Because floating-point types such as
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