Instance Method

clamped(to:)

Returns a copy of this range clamped to the given limiting range.

Declaration

func clamped(to limits: Range<Bound>) -> Range<Bound>

Parameters

limits

The range to clamp the bounds of this range.

Return Value

A new range clamped to the bounds of limits.

Discussion

The bounds of the result are always limited to the bounds of limits. For example:

let x: Range = 0..<20
print(x.clamped(to: 10..<1000))
// Prints "10..<20"

If the two ranges do not overlap, the result is an empty range within the bounds of limits.

let y: Range = 0..<5
print(y.clamped(to: 10..<1000))
// Prints "10..<10"