Integer
Swift provides 8, 16, 32, and 64-Bit Signed and unsigned integer types.
Like other types of SWIFT, the integer type adopts the upper-case naming method.
Integer Range
You can access the min and Max attributes of different integer types to obtain the maximum and minimum values of the corresponding types:
Let minvalue = uint8.min // The value of minvalue is 0, which is the minimum value of uint8 type. Let maxvalue = uint8.max // The value of maxvalue is 255, which is the maximum value of uint8 type.
Int
Uint
Note:
Do not use uint unless you really need to store an unsigned integer with the same native character length as the current platform.
The unified use of int can improve code reusability, avoid conversion between different types of numbers, and match the type inference of numbers.
Type security and type inference
SWIFT is a type-safe language. A type-safe language allows you to clearly understand the type of the value processed by the Code.
Full-value literal
- One decimal number with no prefix
- A binary number with a prefix of 0 B
- An octal number with a prefix of 0 o
- A hexadecimal number with a prefix of 0x
The floating point literal also has an optional index, which is specified in the decimal floating point number by the upper or lower case e, and in the hexadecimal floating point number by the upper or lower case p.
The numeric literal can include additional formats to enhance readability. Both integers and floating-point numbers can be added with extra zeros and underscores, without affecting the literal volume:
let paddedDouble = 000123.456let oneMillion = 1_000_000let justOverOneMillion = 1_000_000.000_000_1
Integer and floating point conversion
Note:
The combination of numeric constants and variables is different from the combination of numeric literal. The literal volume can be directly added to the word surface volume. They do not have a specific type and can only be inferred when the compiler requires a value.