Generic Structure

ClosedRange

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

Overview

You create instances of ClosedRange by using the closed range operator (...).

let lowercase = "a"..."z"

You can use a ClosedRange instance to quickly check if a value is contained in a particular range of values. For example:

print(lowercase.contains("c"))      // Prints "true"
print(lowercase.contains("5"))      // Prints "false"
print(lowercase.contains("z"))      // Prints "true"

Unlike Range, instances of ClosedRange cannot represent an empty interval.

let lowercaseA = "a"..."a"
print(lowercaseA.isEmpty)
// Prints "false"

Topics

Creating a Range

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

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

Returns a closed range that contains both of its bounds.

Beta

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.

Clamping a Range

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

Creates an instance with the given bounds.

func clamped(to: ClosedRange<Bound>)

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

Comparing Ranges

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

Returns a Boolean value indicating whether two ranges are equal.

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

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

Beta
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(CountableClosedRange<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.

Describing a Range

var description: String

A textual representation of the range.

var debugDescription: String

A textual representation of the range, suitable for debugging.

Relationships

Generic Constraints

  • Bound : Comparable
    

See Also

Ranges

struct Range

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software