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 Closed​Range.

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

Symbols

Initializers

init(Range<Bound>)

Creates an instance equivalent to the given range.

init(Closed​Range<Bound>)

Creates an instance equivalent to the given range.

init(Countable​Range<Bound>)

Creates an instance equivalent to the given range.

init(Countable​Closed​Range<Bound>)

Creates an instance equivalent to the given range.

init(unchecked​Bounds:​ (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 custom​Mirror:​ Mirror
var debug​Description:​ String

A textual representation of the range, suitable for debugging.

var description:​ String

A textual representation of the range.

var is​Empty:​ Bool

A Boolean value indicating whether the range contains no elements.

let lower​Bound:​ Bound

The range’s lower bound.

let upper​Bound:​ Bound

The range’s upper bound.

Instance Methods

func clamped(to:​ Range<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(Closed​Range<Bound>)

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

func overlaps(Countable​Range<Bound>)

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

func overlaps(Countable​Closed​Range<Bound>)

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

Operator Functions

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

Returns a Boolean value indicating whether two ranges are equal.

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

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

See Also

Related Symbols

Countable​Range, Closed​Range, Countable​Closed​Range