Creates a new instance with the representable value that’s closest to the given integer.
init<Other>(clamping source: Other) where Other : BinaryInteger
An integer to convert to this type.
If the value passed as
source is greater than the maximum representable value in this type, the result is the type’s
max value. If
source is less than the smallest representable value in this type, the result is the type’s
In this example,
x is initialized as an
Int8 instance by clamping
500 to the range
y is initialized as a
UInt instance by clamping
-500 to the range
let x = Int8(clamping: 500) // x == 127 // x == Int8.max let y = UInt(clamping: -500) // y == 0