A half-open range that forms a collection of consecutive values.

SDK

Xcode 8.0+

Framework

Swift Standard Library

Overview

You create a CountableRange instance by using the half-open range operator (..<).

The associated Bound type is both the element and index type of CountableRange. Each element of the range is its own corresponding index. The lower bound of a CountableRange instance is its start index, and the upper bound is its end index.

If the Bound type has a maximal value, it can serve as an upper bound but can never be contained in a CountableRange<Bound> instance. For example, a CountableRange<Int8> instance can use Int8.max as its upper bound, but it can’t represent a range that includes Int8.max.

If you need to create a range that includes the maximal value of its Bound type, see the CountableClosedRange type.

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 Float and 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 an interval bound by floating-point values, see the Range type. If you need to iterate over consecutive floating-point values, see the stride(from:to:by:) function.

Integer Index Ambiguity

Because each element of a CountableRange instance is its own index, for the range (-99..<100) the element at index 0 is 0. This is an unexpected result for those accustomed to zero-based collection indices, who might expect the result to be -99. To prevent this confusion, in a context where Bound is known to be an integer type, subscripting directly is a compile-time error:

However, subscripting that range still works in a generic context:

Topics

Creating a Range

Create a new range using the half-open range operator (..<).

Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements, using the given predicate as the equivalence test.

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.

Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.

A closed range that forms a collection of consecutive values.

Related Documentation

CountableClosedRange, Range, ClosedRange

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.