Contents

contains(_:)

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

Declaration

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

Parameters

  • other:

    A range to check for containment within this range.

Return Value

true if other is empty or wholly contained within this range; otherwise, false.

Discussion

The given range is contained within this range if its bounds are equal to or within the bounds of this range.

let range = 0..<10
range.contains(2..<5)        // true
range.contains(2..<10)       // true
range.contains(2..<12)       // false

Additionally, passing any empty range as other results in the value true, even if the empty range’s bounds are outside the bounds of this range.

let emptyRange = 3..<3
emptyRange.contains(3..<3)   // true
emptyRange.contains(5..<5)   // true