Generic Function


Returns the given integer as the equivalent value in a different integer type.


func numericCast<T, U>(_ x: T) -> U where T : BinaryInteger, U : BinaryInteger



The integer to convert, and instance of type T.

Return Value

The value of x converted to type U.


The numericCast(_:) function traps on overflow in -O and -Onone builds.

You can use numericCast(_:) to convert a value when the destination type can be inferred from the context. In the following example, the random(in:) function uses numericCast(_:) twice to convert the argument and return value of the arc4random_uniform(_:) function to the appropriate type.

func random(in range: Range<Int>) -> Int {
    return numericCast(arc4random_uniform(numericCast(range.count)))
        + range.lowerBound

let number = random(in: -10...<10)
// number == -3, perhaps