numericCast

func numericCast(_:)(_ x: T) -> U

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

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
  • Parameter x: The integer to convert, and instance of type T.