Generic Structure

CountableClosedRange

A closed range that forms a collection of consecutive values.

Overview

You create a CountableClosedRange instance by using the closed range operator (...).

let throughFive = 0...5

A CountableClosedRange instance contains both its lower bound and its upper bound.

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

Because a closed range includes its upper bound, a closed range whose lower bound is equal to the upper bound contains one element. Therefore, a CountableClosedRange instance cannot represent an empty range.

let zeroInclusive = 0...0
print(zeroInclusive.isEmpty)
// Prints "false"
print(zeroInclusive.count)
// Prints "1"

You can use a for-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.

for n in throughFive.suffix(3) {
    print(n)
}
// Prints "3"
// Prints "4"
// Prints "5"

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

Topics

Type Aliases

typealias CountableClosedRange.Element

The element type of the range; the same type as the range’s bounds.

typealias CountableClosedRange.Index

A type that represents a position in the range.

Initializers

init(ClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableRange<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 debugDescription: String

A textual representation of the range, suitable for debugging.

var description: String

A textual representation of the range.

var endIndex: ClosedRangeIndex<Bound>

The range’s “past the end” position—that is, the position one greater than the last valid subscript argument.

var first: Bound?

The first element of the collection.

var indices: DefaultRandomAccessIndices<CountableClosedRange<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<CountableClosedRange<Bound>>

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

var lazy: LazyCollection<CountableClosedRange<Bound>>

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

var lazy: LazySequence<CountableClosedRange<Bound>>

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

var lazy: LazyRandomAccessCollection<CountableClosedRange<Bound>>

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

let lowerBound: Bound

The range’s lower bound.

var startIndex: ClosedRangeIndex<Bound>

The position of the first element in the range.

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: CountableClosedRange<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 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 in the same order, 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 forEach((Bound) -> Void)

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

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 sequence of sequences, concatenated.

func joined()

Returns the elements of this collection of collections, concatenated.

func joined(separator: String)

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

func joined<Separator>(separator: Separator)

Returns the concatenated elements of this sequence of sequences, inserting 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 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(ClosedRange<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(CountableRange<Bound>)

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

func prefix(Int)

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

func prefix(through: ClosedRangeIndex<Bound>)

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

func prefix(upTo: ClosedRangeIndex<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 reduce<Result>(into: Result, (inout Result, Bound) -> ())

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

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: ClosedRangeIndex<Bound>)

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

Subscripts

subscript(ClosedRangeIndex<Bound>)

Accesses the element at specified position.

subscript(Range<ClosedRangeIndex<Bound>>)

Accesses a contiguous subrange of the collection’s elements.

Operator Functions

static func !=(CountableClosedRange<Bound>, CountableClosedRange<Bound>)

Returns a Boolean value indicating whether two values are not equal.

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

Returns a Boolean value indicating whether two ranges are equal.

static func ~=(CountableClosedRange<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

Ranges

func ..<<Bound>(Bound, Bound)

Returns a countable half-open range that contains its lower bound but not its upper bound.

struct CountableRange

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

func ...<Bound>(Bound, Bound)

Returns a countable closed range that contains both of its bounds.