Generic Structure

Range

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

Overview

You create Range instances 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:

print(underFive.contains(3.14))     // Prints "true"
print(underFive.contains(6.28))     // Prints "false"
print(underFive.contains(5.0))      // Prints "false"

Range instances can represent an empty interval, unlike ClosedRange.

let empty = 0.0..<0.0
print(empty.contains(0.0))          // Prints "false"
print(empty.isEmpty)                // Prints "true"

Topics

Creating a Range

Create a new range using the half-open range operator (..<).

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

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

Converting Ranges

init(Range<Bound>)

Creates an instance equivalent to the given range.

init(ClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableClosedRange<Bound>)

Creates an instance equivalent to the given range.

Inspecting a Range

var isEmpty: Bool

A Boolean value indicating whether the range contains no elements.

var count: Bound.Stride

The number of values contained in the range.

let lowerBound: Bound

The range’s lower bound.

let upperBound: Bound

The range’s upper bound.

Checking for Containment

func contains(Bound)

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

static func ~=(Range<Bound>, Bound)

Returns a Boolean value indicating whether a value is included in a range.

Working with Foundation Ranges

Clamping a Range

func clamped(to: Range<Bound>)

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

Comparing Ranges

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

Returns a Boolean value indicating whether two ranges are equal.

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

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

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(CountableRange<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.

Describing a Range

var description: String

A textual representation of the range.

var debugDescription: String

A textual representation of the range, suitable for debugging.

Infrequently Used Functionality

init(uncheckedBounds: (lower: Bound, upper: Bound))

Creates an instance with the given bounds.

Relationships

Generic Constraints

  • Bound : Comparable
    

See Also

Ranges

struct ClosedRange

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