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"

Symbols

Initializers

init(CountableClosedRange<Bound>)

Creates an instance equivalent to the given range.

init(CountableRange<Bound>)

Creates an instance equivalent to the given range.

init(Range<Bound>)

Creates an instance equivalent to the given range.

init(ClosedRange<Bound>)

Creates an instance equivalent to the given range.

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

Creates an instance with the given bounds.

Instance Properties

var count: Bound.Stride

The number of values contained in the range.

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.

let lowerBound: Bound

The range’s lower bound.

let upperBound: Bound

The range’s upper bound.

Instance Methods

func clamped(to: ClosedRange<Bound>)

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

func contains(Bound)

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

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.

Operator Functions

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

Returns a Boolean value indicating whether two ranges are equal.

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

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

Relationships

Generic Constraints

  • Bound : Comparable

See Also

Related Symbols

CountableRange, Range, CountableClosedRange