Generic Structure

CountableRange

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

Overview

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

let upToFive = 0..<5

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.

print(upToFive.contains(3))         // Prints "true"
print(upToFive.contains(10))        // Prints "false"
print(upToFive.contains(5))         // Prints "false"

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.

let maximumRange = Int8.min..<Int8.max
print(maximumRange.contains(Int8.max))
// Prints "false"

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:

// error: ambiguous use of 'subscript'
print((-99..<100)[0])

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

func brackets<T>(_ x: CountableRange<T>, _ i: T) -> T {
    return x[i] // Just forward to subscript
}
print(brackets(-99..<100, 0))
// Prints "0"

Symbols

Type Aliases

Element

The bound type of the range.

Index

A type that represents a position in the range.

IndexDistanceIndicesSubSequence

Initializers

init(CountableRange<Bound>)

Creates an instance equivalent to the given range.

init(ClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(Range<Bound>)

Creates an instance equivalent to the given range.

init(uncheckedBounds: (lower: Bound, upper: Bound))

Creates an instance with the given bounds.

Instance Properties

var count: Bound.Stride

The number of elements in the collection.

var customMirror: Mirror
var debugDescription: String

A textual representation of the range, suitable for debugging.

var description: String

A textual representation of the range.

var endIndex: Bound
var first: Bound?

The first element of the collection.

var indices: CountableRange<Bound>

The indices that are valid for subscripting the range, in ascending order.

var indices: DefaultRandomAccessIndices<CountableRange<Bound>>

The indices that are valid for subscripting the collection, in ascending order.

var indices: CountableRange<Bound>

The indices that are valid for subscripting the collection, in ascending order.

var isEmpty: Bool

A Boolean value indicating whether the collection is empty.

var isEmpty: Bool

A Boolean value indicating whether the range contains no elements.

var last: Bound?

The last element of the collection.

var lazy: LazyBidirectionalCollection<CountableRange<Bound>>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazyCollection<CountableRange<Bound>>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazyRandomAccessCollection<CountableRange<Bound>>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazySequence<CountableRange<Bound>>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

let lowerBound: Bound

The range’s lower bound.

var startIndex: Bound
var underestimatedCount: Int

Returns a value less than or equal to the number of elements in the sequence, nondestructively.

var underestimatedCount: Int

A value less than or equal to the number of elements in the collection.

let upperBound: Bound

The range’s upper bound.

Instance Methods

func clamped(to: CountableRange<Bound>)

Returns a copy of this range clamped to the given limiting range.

func contains(Bound)

Returns a Boolean value indicating whether the sequence contains the given element.

func contains(where: (Bound) -> Bool)

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

func distance(from: CountableRange.Index, to: CountableRange.Index)
func drop(while: (Bound) -> Bool)

Returns a subsequence by skipping elements while predicate returns true and returning the remaining elements.

func dropFirst()

Returns a subsequence containing all but the first element of the sequence.

func dropFirst(Int)

Returns a subsequence containing all but the given number of initial elements.

func dropLast()

Returns a subsequence containing all but the last element of the sequence.

func dropLast(Int)

Returns a subsequence containing all but the specified number of final elements.

func elementsEqual<OtherSequence>(OtherSequence)

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func elementsEqual<OtherSequence>(OtherSequence, by: (Bound, Bound) -> Bool)

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

func enumerated()

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.

func filter((Bound) -> Bool)

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func first(where: (Bound) -> Bool)

Returns the first element of the sequence that satisfies the given predicate.

func flatMap<SegmentOfResult>((Bound) -> SegmentOfResult)

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.

func flatMap<ElementOfResult>((Bound) -> ElementOfResult?)

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func forEach((Bound) -> Void)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

func index(CountableRange.Index, offsetBy: CountableRange.IndexDistance)func index(after: CountableRange.Index)func index(before: CountableRange.Index)
func index(of: Bound)

Returns the first index where the specified value appears in the collection.

func index(where: (Bound) -> Bool)

Returns the first index in which an element of the collection satisfies the given predicate.

func joined()

Returns the elements of this collection of collections, concatenated.

func joined()

Returns the elements of this collection of collections, concatenated.

func joined()

Returns the elements of this sequence of sequences, concatenated.

func joined<Separator>(separator: Separator)

Returns the concatenated elements of this sequence of sequences, inserting the given separator between each element.

func joined(separator: String)

Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence)

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

func lexicographicallyPrecedes<OtherSequence>(OtherSequence, by: (Bound, Bound) -> Bool)

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

func makeIterator()

Returns an iterator over the elements of the collection.

func map<T>((Bound) -> T)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func map<T>((Bound) -> T)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func max()

Returns the maximum element in the sequence.

func max(by: (Bound, Bound) -> Bool)

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min()

Returns the minimum element in the sequence.

func min(by: (Bound, Bound) -> Bool)

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func overlaps(Range<Bound>)

Returns a Boolean value indicating whether this range and the given range contain an element in common.

func overlaps(CountableClosedRange<Bound>)

Returns a Boolean value indicating whether this range and the given range contain an element in common.

func overlaps(ClosedRange<Bound>)

Returns a Boolean value indicating whether this range and the given range contain an element in common.

func overlaps(CountableRange<Bound>)

Returns a Boolean value indicating whether this range and the given range contain an element in common.

func popFirst()

Removes and returns the first element of the collection.

func popLast()

Removes and returns the last element of the collection.

func prefix(Int)

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(through: Bound)

Returns a subsequence from the start of the collection through the specified position.

func prefix(upTo: Bound)

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func prefix(while: (Bound) -> Bool)

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func reduce<Result>(Result, (Result, Bound) -> Result)

Returns the result of combining the elements of the sequence using the given closure.

func removeFirst()

Removes and returns the first element of the collection.

func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

func removeLast()

Removes and returns the last element of the collection.

func removeLast(Int)

Removes the given number of elements from the end of the collection.

func reversed()

Returns a view presenting the elements of the collection in reverse order.

func sorted()

Returns the elements of the sequence, sorted.

func sorted(by: (Bound, Bound) -> Bool)

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Bound) -> Bool)

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

func split(separator: Bound, maxSplits: Int, omittingEmptySubsequences: Bool)

Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.

func starts<PossiblePrefix>(with: PossiblePrefix)

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func starts<PossiblePrefix>(with: PossiblePrefix, by: (Bound, Bound) -> Bool)

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.

func suffix(Int)

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from: Bound)

Returns a subsequence from the specified position to the end of the collection.

Subscripts

subscript(Range<Bound>)

Accesses the subsequence bounded by the given range.

subscript(ClosedRange<Bound>)

Accesses the subsequence bounded by the given range.

subscript(CountableRange<Bound>)

Accesses the subsequence bounded by the given range.

subscript(CountableClosedRange<Bound>)

Accesses the subsequence bounded by the given range.

subscript(Bound)

Accesses the element at specified position.

Operator Functions

static func ==(CountableRange<Bound>, CountableRange<Bound>)

Returns a Boolean value indicating whether two ranges are equal.

static func ~=(CountableRange<Bound>, Bound)

Returns a Boolean value indicating whether a value is included in a range.

Relationships

Generic Constraints

  • Bound : Comparable
  • Bound : _Strideable
  • Bound.Stride : SignedInteger

See Also

Related Symbols

CountableClosedRange, Range, ClosedRange