Contents

contains(_:)

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

Declaration

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

Parameters

  • other:

    A range to check for containment within this closed range.

Return Value

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

Discussion

The given range is contained within this closed range if the elements of the range are all contained within this closed range.

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

// Note that `5..<11` contains 5, 6, 7, 8, 9, and 10.
range.contains(5..<11)    // true

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 closed range.

range.contains(3..<3)     // true
range.contains(20..<20)   // true