Range

struct Range

A half-open interval from a lower bound up to, but not including, an upper bound.

Inheritance CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable, RangeExpression

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.

Initializers

init init(uncheckedBounds:) Required

Creates an instance with the given bounds.

Because this initializer does not perform any checks, it should be used as an optimization only when you are absolutely certain that lower is less than or equal to upper. Using the half-open range operator (..<) to form Range instances is preferred.

  • Parameter bounds: A tuple of the lower and upper bounds of the range.

Declaration

@inlinable public init(uncheckedBounds bounds: (lower: Bound, upper: Bound))

Instance Variables

var customMirror Required

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

var customMirror: Mirror
var debugDescription Required

A textual representation of the range, suitable for debugging.

Declaration

var debugDescription: String
var description Required

A textual representation of the range.

Declaration

var description: String
var isEmpty Required

A Boolean value indicating whether the range contains no elements.

An empty Range instance has equal lower and upper bounds.

let empty: Range = 10..<10
print(empty.isEmpty)
// Prints "true"

Declaration

var isEmpty: Bool
let lowerBound Required

The range's lower bound.

In an empty range, lowerBound is equal to upperBound.

Declaration

let lowerBound: Bound
let upperBound Required

The range's upper bound.

In an empty range, upperBound is equal to lowerBound. A Range instance does not contain its upper bound.

Declaration

let upperBound: Bound

Instance Methods

func clamped(to limits: Range<Bound>) -> Range<Bound> Required

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

The bounds of the result are always limited to the bounds of limits. For example:

let x: Range = 0..<20
print(x.clamped(to: 10..<1000))
// Prints "10..<20"

If the two ranges do not overlap, the result is an empty range within the bounds of limits.

let y: Range = 0..<5
print(y.clamped(to: 10..<1000))
// Prints "10..<10"
  • Parameter limits: The range to clamp the bounds of this range.

Declaration

@inlinable public func clamped(to limits: Range<Bound>) -> Range<Bound>
func contains(_ element: Bound) -> Bool Required

Returns a Boolean value indicating whether the given element is contained within the range.

Because Range represents a half-open range, a Range instance does not contain its upper bound. element is contained in the range if it is greater than or equal to the lower bound and less than the upper bound.

  • Parameter element: The element to check for containment.

Declaration

@inlinable public func contains(_ element: Bound) -> Bool
func overlaps(_ other: Range<Bound>) -> Bool Required

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

This example shows two overlapping ranges:

let x: Range = 0..<20
print(x.overlaps(10...1000))
// Prints "true"

Because a half-open range does not include its upper bound, the ranges in the following example do not overlap:

let y = 20..<30
print(x.overlaps(y))
// Prints "false"
  • Parameter other: A range to check for elements in common.

Declaration

@inlinable public func overlaps(_ other: Range<Bound>) -> Bool
func overlaps(_ other: ClosedRange<Bound>) -> Bool Required

Declaration

@inlinable public func overlaps(_ other: ClosedRange<Bound>) -> Bool
func relative(to collection: C) -> Range<Bound> Required

Returns the range of indices described by this range expression within the given collection.

  • Parameter collection: The collection to evaluate this range expression in relation to.

Declaration

@inlinable public func relative<C>(to collection: C) -> Range<Bound> where Bound == C.Index, C: Collection

Type Methods

func !=(lhs: Self, rhs: Self) -> Bool Required

Declaration

public static func !=(lhs: Self, rhs: Self) -> Bool
func ==(lhs: Range<Bound>, rhs: Range<Bound>) -> Bool Required

Returns a Boolean value indicating whether two ranges are equal.

Two ranges are equal when they have the same lower and upper bounds. That requirement holds even for empty ranges.

let x = 5..<15
print(x == 5..<15)
// Prints "true"

let y = 5..<5
print(y == 15..<15)
// Prints "false"

Declaration

@inlinable public static func ==(lhs: Range<Bound>, rhs: Range<Bound>) -> Bool
func ~=(pattern: Self, value: Self.Bound) -> Bool Required

Declaration

@inlinable public static func ~=(pattern: Self, value: Self.Bound) -> Bool