Basics: Integers

  • Are either signed (positive, zero, or negative) or unsigned (positive or zero).
  • Swift provides signed and unsigned integers in 8, 16, 32, and 64 bit forms.
  • These follow a naming convention similar to C, in that an 8-bit unsigned integer is of type UInt8, and a 32-bit signed integer is of type Int32.

Additional information

  • Are whole numbers with no fractional component.

Integer Bounds

Let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8

You can access the minimum and maximum values of each integer type with its min and max properties.

Additional information

  • The values of these properties are of the appropriate-sized number type and can therefore be used in expressions alongside other values of the same type.

Int

Swift provides an additional integer type, Int, which has the same size as the current platform’s native word size.

Additional information

  • Always use it for integer values in your code.
  • Can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.

UInt

Use this only when you specifically need an unsigned integer type with the same size as the platform’s native word size.

Unsigned integer type.

Additional information

  • Has the same size as the current platform’s native word size.

Basics: Integers — Structure map

Clickable & Draggable!

Basics: Integers — Related pages: