An interval from a lower bound up to, and including, an upper bound.

SDK

Xcode 8.0+

Framework

Swift Standard Library

Declaration

Overview

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

A ClosedRange 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 ClosedRange 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 for-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 Float and 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 stride(from:through:by:) function.

Topics

Creating a Range

Create a new range using the closed range operator (...).

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.

Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

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.

Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

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.

Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

Call body(p), where p is a pointer to the collection’s contiguous storage. If no such storage exists, it is first created. If the collection does not support an internal representation in a form of contiguous storage, body is not called and nil is returned.

Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

Returns the difference needed to produce this collection’s ordered elements from the given collection, using the given predicate as an equivalence test.

Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.