Structureswift 6.0.1Swift
ClosedRange
An interval from a lower bound up to, and including, an upper bound.
@frozen struct ClosedRange<Bound> where Bound : Comparable
You create a ClosedRange
instance by using the closed range operator (...
).
let throughFive = 0...5
A ClosedRange
instance contains both its lower bound and its upper bound.
throughFive.contains(3)
// true
throughFive.contains(10)
// false
throughFive.contains(5)
// true
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.
let zeroInclusive = 0...0
zeroInclusive.contains(0)
// true
zeroInclusive.isEmpty
// false
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.
for n in 3...5 {
print(n)
}
// Prints "3"
// Prints "4"
// Prints "5"
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.
Citizens in Swift
Type members
init(uncheckedBounds: (lower: Bound, upper: Bound)
) Creates an instance with the given bounds.
Instance members
let lowerBound: Bound
The range’s lower bound.
let upperBound: Bound
The range’s upper bound.
Citizens in Swift
where Bound:Encodable, Bound:Comparable
Conformances
protocol Encodable
A type that can encode itself to an external representation.
Instance members
Citizens in Swift
where Bound:Hashable, Bound:Comparable
Conformances
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.
Instance members
Citizens in Swift
where Bound:Comparable
Conformances
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomReflectable
A type that explicitly supplies its own mirror.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol RangeExpression<Bound>
A type that can be used to slice a collection.
Type members
static func == (lhs: ClosedRange<Bound>, rhs: ClosedRange<Bound>
) -> Bool Returns a Boolean value indicating whether two ranges are equal.
Instance members
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 isEmpty: Bool
A Boolean value indicating whether the range contains no elements.
func clamped(to: ClosedRange<Bound>
) -> ClosedRange<Bound> Returns a copy of this range clamped to the given limiting range.
func contains(Bound
) -> Bool Returns a Boolean value indicating whether the given element is contained within the range.
func overlaps(ClosedRange<Bound>
) -> Bool func overlaps(Range<Bound>
) -> Bool func relative<C>(to: C
) -> Range<Bound>
Type features
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
static func ~= (pattern: Self, value: Self.Bound
) -> Bool Returns a Boolean value indicating whether a value is included in a range.
Citizens in Swift
where Bound:Comparable, Bound:Decodable
Conformances
protocol Decodable
A type that can decode itself from an external representation.
Type members
Citizens in Swift
where Bound:Comparable, Bound:Sendable
Conformances
Citizens in Swift
where Bound:Strideable, Bound.Stride:SignedInteger
Conformances
protocol BidirectionalCollection<Element>
A collection that supports backward as well as forward traversal.
protocol Collection<Element>
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol RandomAccessCollection<Element>
A collection that supports efficient random-access index traversal.
protocol Sequence<Element>
A type that provides sequential, iterated access to its elements.
Types
Typealiases
Type members
init(Range<Bound>
) Creates an instance equivalent to the given
Range
.
Show obsolete interfaces (1)
Hide obsolete interfaces
init(ClosedRange<Bound>
) Now that Range is conditionally a collection when Bound: Strideable, CountableRange is no longer needed. This is a deprecated initializer for any remaining uses of Range(countableRange).
Instance members
var endIndex: ClosedRange<Bound>.Index
The range’s “past the end” position—that is, the position one greater than the last valid subscript argument.
var startIndex: ClosedRange<Bound>.Index
The position of the first element in the range.
subscript(Range<ClosedRange<Bound>.Index>
) -> Slice<ClosedRange<Bound>> subscript(ClosedRange<Bound>.Index
) -> Bound Accesses the element at specified position.
func distance(from: ClosedRange<Bound>.Index, to: ClosedRange<Bound>.Index
) -> Int func index(ClosedRange<Bound>.Index, offsetBy: Int
) -> ClosedRange<Bound>.Index func index(after: ClosedRange<Bound>.Index
) -> ClosedRange<Bound>.Index func index(before: ClosedRange<Bound>.Index
) -> ClosedRange<Bound>.Index
Extension in Testing
where Bound:Comparable
Conformances
protocol CustomTestStringConvertible
A protocol describing types with a custom string representation when presented as part of a test’s output.
Instance members
Extension in Testing
where Bound == Int
Conformances
Show system interfaces (1)
Hide system interfaces
protocol ExpectedCount
A protocol that describes a range expression that can be used with
confirmation(_:expectedCount:sourceLocation:_:)
.