Signed integers: Int, Int8, Int16, Int32, Int64

Unsigned type: UInt, UInt8, UInt16, UInt32, UInt64

Int and UInt characters are always the same length as the native characters on the current platform, which means that a 32-bit integer is declared on a 32-bit system and a 64-bit integer is declared on a 64-bit system.

Each data type has its own fixed range of values that can be represented or stored.

Get the lowest and maximum values of the UInt8 type by calling the min and Max properties inside the UInt8

Attributes: min and Max

UInt8.max

UInt8.min

The length of the integer type is the same as the native word length of the current platform:

On 32-bit platforms, Int and Int32 have the same length

On 64-bit platforms, Int and Int64 have the same length

The general Int type is sufficient

Unsigned type

The length of an unsigned type is the same as the length of the native word for the current platform:

On 32-bit platforms, UInt and UInt32 have the same length

On a 64-bit platform, UInt and UInt64 have the same length

UInt.max

UInt.min

The explicit declaration

To declare an integer variable, you simply need to follow the variable name with the integer type name, separated by a colon. Such as:

var x:Int

Type inference

You can also assign an integer literal directly, and type inference will automatically specify the type for the variable.

var y = 12

The above declaration uses an implicit declaration mechanism that automatically calls the corresponding constructor, and the declaration form is actually like this:

var z = Int.init(12)

var xyz:Int = 12

Note: Try not to use UInt unless you need to store an unsigned integer of the same length as the current platform word

floating-point

Floating-point is also one of the most common basic types, and Swift provides us with two signed floating-point types, Double and Float. Float is a 32-bit floating-point type and Double is a 64-bit floating-point type. Double can display floating-point numbers with higher precision than Float.

Note: When declaring a float variable or constant using type inference, the variable or constant is always inferred to be of type Double by default.

Type conversion

A float is cast to an integer

var a = 12

Var b = 12.0

a = Int(b)

An integer is cast to a string

var c = 12

var s = String(c)

A string is cast to an integer

var str1 = “Hello World”

var d = Int(str1)