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"

Symbols

Initializers

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.

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

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.

Relationships

Generic Constraints

  • Bound : Comparable

See Also

Related Symbols

CountableRange, ClosedRange, CountableClosedRange