Range
A half-open interval from a lower bound up to, but not including, an upper bound.
@frozen struct Range<Bound> where Bound : Comparable
You create a Range
instance by using the half-open range operator (..<
).
let underFive = 0.0..<5.0
You can use a Range
instance to quickly check if a value is contained in a particular range of values. For example:
underFive.contains(3.14)
// true
underFive.contains(6.28)
// false
underFive.contains(5.0)
// false
Range
instances can represent an empty interval, unlike ClosedRange
.
let empty = 0.0..<0.0
empty.contains(0.0)
// false
empty.isEmpty
// true
Using a Range as a Collection of Consecutive Values
When a 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, but not including, its upper bound.
for n in 3..<5 {
print(n)
}
// Prints "3"
// Prints "4"
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:to:by:)
function.
Citizens in Swift
Members
init(uncheckedBounds: (lower: Bound, upper: Bound)
) Creates an instance with the given bounds.
var isEmpty: Bool
A Boolean value indicating whether the range contains no elements.
let lowerBound: Bound
The range’s lower bound.
let upperBound: Bound
The range’s upper bound.
func contains(Bound
) -> Bool Returns a Boolean value indicating whether the given element is contained within the range.
Citizens in Swift
where Bound:Strideable, Bound.Stride:SignedInteger
Conformances
protocol BidirectionalCollection
A collection that supports backward as well as forward traversal.
protocol Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol RandomAccessCollection
A collection that supports efficient random-access index traversal.
protocol Sequence
A type that provides sequential, iterated access to its elements.
Members
init(ClosedRange
<Bound>) Creates an instance equivalent to the given
ClosedRange
.var endIndex: Range<Bound>.Index
var indices: Range<Bound>.Indices
The indices that are valid for subscripting the range, in ascending order.
var startIndex: Range<Bound>.Index
subscript(Range
<Range<Bound>.Index>) -> Range<Bound> Accesses the subsequence bounded by the given range.
subscript(Range
<Bound>.Index) -> Range<Bound>.Element Accesses the element at specified position.
func distance(from: Range<Bound>.Index, to: Range<Bound>.Index
) -> Int func index(Range
<Bound>.Index, offsetBy: Int) -> Range<Bound>.Index func index(after: Range<Bound>.Index
) -> Range<Bound>.Index func index(before: Range<Bound>.Index
) -> Range<Bound>.Index typealias Element
typealias Index
A type that represents a position in the range.
typealias Indices
typealias Iterator
typealias SubSequence
init(Range
<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).
Citizens in Swift
where Bound:Comparable
Conformances
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 RangeExpression
A type that can be used to slice a collection.
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.
static func == (Range
<Bound>, Range<Bound>) -> Bool Returns a Boolean value indicating whether two ranges are equal.
func clamped(to: Range<Bound>
) -> Range<Bound> Returns a copy of this range clamped to the given limiting range.
func overlaps(ClosedRange
<Bound>) -> Bool func overlaps(Range
<Bound>) -> Bool Returns a Boolean value indicating whether this range and the given range contain an element in common.
func relative<C>(to: C
) -> Range<Bound> Returns the range of indices described by this range expression within the given collection.
Features
static func != (Self, Self
) -> Bool static func ~= (Self, 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.
Members
Citizens in Swift
where Bound:Comparable, Bound:Encodable
Conformances
protocol Encodable
A type that can encode itself to an external representation.
Members
Citizens in Swift
where Bound:Comparable, Bound:Sendable
Conformances
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Citizens in Swift
where Bound:Comparable, Bound:Hashable
Conformances
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.
Members
func hash(into: inout Hasher
) Hashes the essential components of this value by feeding them into the given hasher.
Available in _RegexParser
where Bound:Comparable
Members
Available in Foundation
where Bound:BinaryInteger
Members
Available in Foundation
where Bound == AttributedString.Index
Members
Available in Foundation
where Bound == String.Index
Members
Available in Foundation
where Bound == Int
Members
Extension in Markdown
where Bound == SourceLocation
Members
func diagnosticDescription(includePath: Bool
) -> String A textual description for use in diagnostics.
init(start: Bound, end: Bound
) var end: SourceLocation
var start: SourceLocation