Instance Method

overlaps(_:)

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

Declaration

func overlaps(_ other: Range<Bound>) -> Bool

Parameters

other

A range to check for elements in common.

Return Value

true if this range and other have at least one element in common; otherwise, false.

Discussion

This example shows two overlapping ranges:

let x: ClosedRange = 0...20
print(x.overlaps(10..<1000 as Range))
// Prints "true"

Because a closed range includes its upper bound, the ranges in the following example also overlap:

let y: Range = 20...30
print(x.overlaps(y))
// Prints "true"

See Also

Comparing Ranges

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

Returns a Boolean value indicating whether two ranges are equal.

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

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

func overlaps(ClosedRange<Bound>) -> Bool

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