Numeric Type Conversion: Integer Conversion

The range of numbers that can be stored in an integer constant or variable is different for each numeric type.

Let cannotBeNegative: UInt8 = -1
// UInt8 cannot store negative numbers, and so this will report an error
let tooBig: Int8 = Int8.max + 1
// Int8 cannot store a number larger than its maximum value,
// and so this will also report an error
Let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

Additional information

  • A number that won’t fit into a constant or variable of a sized integer type is reported as an error when your code is compiled.
  • To convert one specific number type to another, you initialize a new number of the desired type with the existing value.

Numeric Type Conversion: Integer Conversion — Structure map

Clickable & Draggable!

Numeric Type Conversion: Integer Conversion — Related pages: