Swift data types


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

The data type of a variable determines how the bits representing those values ​​are stored in the computer's memory. You can also specify the data type of a variable when declaring it.

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


Built-in data types

Swift provides a very rich data type. The following lists several commonly used data types:

Int

In general, you don't need to specifically specify the length of the integer. Swift provides a special integer type Int, whose length 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 are the same length.

Unless you need an integer of a specific length, generally using Int is enough. This improves code consistency and reusability. Even on 32-bit platforms, the range of integers that can be stored in Int can reach -2,147,483,648~2,147,483,647, which is large enough most of the time.

UInt

Swift also provides a special unsigned type UInt, whose length is the same as the native word length of the current platform:

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

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

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's better to use Int, even if the value you want to store is known to be non-negative. Uniform use of Int can improve code reusability, avoid conversions between different types of numbers, and match the type inference of numbers. Please refer to Type Safety and Type Inference.

Floating point numbers

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

The floating point type represents a larger range than the integer type and can store larger or smaller numbers than the Int type. Swift provides two signed floating point number types:

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

  • Float represents a 32-bit floating point number. This type can be used if the accuracy requirements are not high.

Note:
Double is very accurate, with at least 15 digits, while Float has at least 6 digits number. Which type you choose depends on the range of values ​​your code needs to handle.

Boolean value

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

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

"Hello, World!"

Character

Character refers to a single letter, for example:

"C"

Optional type

Use optional types (optionals) to handle situations where values ​​may be missing. Optional types represent either a value or no value.



Value range

The following table shows the storage space of different variable types, and the maximum and minimum values ​​of the variable types:

TypeSize (bytes) Interval value
Int81 byte -127 to 127
UInt81 Bytes 0 to 255
Int324 bytes -2147483648 to 2147483647
UInt324 bytes 0 to 4294967295
Int648 bytes -9223372036854775808 to 9223372036854775807
UInt648 Bytes 0 to 18446744073709551615
Float4 Bytes 1.2E-38 to 3.4E+38 (~6 digits)
Double8 byte2.3E-308 to 1.7E+308 (~15 digits )

Type alias

A type alias defines another name for the current type. Type aliases are defined using the typealias keyword. The syntax format is as follows:

typealias newname = type

For example, the following defines the type alias of Int as Feet:

typealias Feet = Int

Now, we can define variables through aliases:

import Cocoa

typealias Feet = Int
var distance: Feet = 100
print(distance)

We use playground execution The output of the above program is:

100

Type safety

Swift is a type safe language.

Because Swift is type-safe, it performs type checks when compiling your code and marks mismatched types as errors. This allows you to find and fix bugs early during development.

import Cocoa

var varA = 42
varA = "This is hello"
print(varA)

The above program will report an error in Xcode:

error: cannot assign value of type 'String' to type 'Int'
varA = "This is hello"

It means that the 'String' string cannot be assigned to the 'Int' variable.


Type inference

Type checking can help you avoid errors when you have to deal with values ​​of different types. However, this does not mean that you need to explicitly specify the type every time you declare a constant or variable.

If you don’t specify a type explicitly, Swift will use type inference to choose the appropriate type.

For example, if you assign a value of 42 to a new constant and don’t specify a type, Swift can infer that the constant type is Int because the initial value you assign it looks like an integer:

let meaningOfLife = 42
// meaningOfLife 会被推测为 Int 类型

Similarly, if you do not specify a type for a floating-point literal, Swift will infer that you want Double:

let pi = 3.14159
// pi 会被推测为 Double 类型

When inferring the type of a floating-point number, Swift will always choose Double instead of Float.

If both integers and floating-point numbers appear in the expression, they will be inferred as Double type:

let anotherPi = 3 + 0.14159
// anotherPi 会被推测为 Double 类型

The original value 3 does not explicitly declare the type, and a floating-point word appears in the expression literal, so the expression will be inferred to be of type Double.

Example

import Cocoa

// varA 会被推测为 Int 类型 
var varA = 42
print(varA)

// varB 会被推测为 Double 类型  
var varB = 3.14159
print(varB)

// varC 也会被推测为 Double 类型   
var varC = 3 + 0.14159
print(varC)

Execute the above code, the output result is:

42
3.14159
3.14159