Swift data type


Release date:2023-11-23 Update date:2023-12-11 Editor:admin View counts:168

Label:

Swift data type

When we program in any programming language, we need to use a variety of data types to store different information.

The data type of the variable determines how the bits that represent these values are stored in the computer’s memory. You can also specify its data type when you declare a variable.

All variables have data types to determine what kind of data can be stored.

Built-in data type

Swift provides a wide range of data types, several of which are commonly used are listed below:

Int

In general, you do not need to specify the length of an integer. Swift provides a special integer type Int the length is the same as the original word length of the current platform

  • On 32-bit platform Int and Int32 the length is the same.

  • On 64-bit platform Int and Int64 the length is the same.

Unless you need an integer of a specific length, generally use the Int .That’s enough. This can improve code consistency and reusability. Even on a 32-bit platform Int the range of integers that can be stored can also be reached -2,147,483,648 ~ 2,147,483,647 is big enough most of the time.

UInt

Swift also provides a special unsigned type UInt the length is the sameas the original word length of the current platform

  • On 32-bit platform UInt and UInt32 the length is the same.

  • On 64-bit platform UInt and UInt64 the length is the same.

Note: try not to use UInt unless you really need to store an unsigned integer with the same length as the native word length of the current platform Except in this case, it is best to use Int even if the value you want to store is known to be non-negative Unified use Int can improve the reusability of the code, avoid the conversion between different types of numbers, and match the type inference of numbers.

Integer types need to be aware of the following:

  • On 32-bit systems, Int and Int32 are the same length.

  • On 64-bit systems, Int and Int64 are the same length.

  • On 32-bit systems, UInt and UInt32 are the same length.

  • On 64-bit systems, UInt and UInt64 are the same length.

  • Int8, Int16, Int32 and Int64 represent 8-bit, 16-bit, 32-bit, and 64-bit signed integers, respectively.

  • UInt8, UInt16, UInt32, and UInt64 represent 8-bit, 16-bit, 32-bit and 64-bit unsigned integers, respectively.

Floating point numbers: Float, Double

Floating point numbers are numbers with decimal parts, such as 3.14159, 0.1, and -273.15.

Floating-point types represent a wider range than integer types, and can store a larger range than Int a number with a larger or smaller type. Swift provides two types of signed floating point numbers:

  • Double represents a 64-bit floating-point number. Use this type when you need to store large or high-precision floating-point numbers.

  • Float represents a 32-bit floating point number. This type can be used if the precision requirement is not high.

Note: Double is very accurate, at least 15 digits, and Float there are at least six digits. Which type you choose depends on the range ofvalues your code needs to deal with.

Boolean value: Bool

Swift has a basic Boolean type called Bool. Boolean values refer to logical values because they can only be true or false. Swift has two Boolean constants, true and false.

String: String

A string is a sequence collection of characters, for example:

"Hello, World!"

Character:

A character refers to a single letter, such as:

"C"

Optional type:

Use optional types to handle situations where values may be missing. An optional type indicates that there is or no value.

Powered by TorCMS (https://github.com/bukun/TorCMS).