A closed range that forms a collection of consecutive values.
- Xcode 8.0+
- Swift Standard Library
You create a
Countable instance by using the closed range operator (
Countable 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
Countable instance cannot represent an empty range.
You can use a
in loop or any sequence or collection method with a countable range. The elements of the range are the consecutive values from its lower bound up to, and including, its upper bound.
You can create a countable range over any type that conforms to the
Strideable protocol and uses an integer as its associated
Stride type. By default, Swift’s integer and pointer types are usable as the bounds of a countable range.
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 test whether values are contained within a closed interval bound by floating-point values, see the
Closed type. If you need to iterate over consecutive floating-point values, see the