Swift Data Types
When we program in any programming language, we need to use various data types to store different kinds of information.
The data type of a variable determines how the bits representing these values are stored in the computer's memory. You can also specify its data type when declaring a variable.
All variables have a data type to determine which kind of data they can store.
Built-in Data Types
Swift provides a very rich set of data types. The following lists several commonly used data types:
Int
Generally, you don't need to specifically specify the length of integers. Swift provides a special integer typeInt, whose length is the same as the native word size of the current platform:
- On 32-bit platforms,
IntandInt32the length is the same. - On 64-bit platforms,
IntandInt64the length is the same.
Unless you need integers of a specific length, generally useIntis sufficient. This improves code consistency and reusability. Even on a 32-bit platform,Intthe range of integers that can be stored 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 typeUInt, whose length is the same as the native word size of the current platform:
- On 32-bit platforms,
UIntandUInt32the length is the same. - On 64-bit platforms,
UIntandUInt64the length is the same.
Note:Try not to use
UInt, unless you really need to store an unsigned integer with the same size as the current platform's native word length. Except in this case, it's best to useInt, even if the value you want to store is known to be non-negative. Using consistentlyIntcan improve code reusability, avoid conversions between different types of numbers, and match the type inference of numbers.
The following points should be noted about integer types:
On 32-bit systems, Int and Int32 have the same length.
On 64-bit systems, Int and Int64 have the same length.
On 32-bit systems, UInt and UInt32 have the same length.
On 64-bit systems, UInt and UInt64 have the same length.
Int8, Int16, Int32, and Int64 represent 8-bit, 16-bit, 32-bit, and 64-bit signed integer forms, respectively.
UInt8, UInt16, UInt32, and UInt64 represent 8-bit, 16-bit, 32-bit, and 64-bit unsigned integer forms, respectively.
Floating-Point Numbers: Float, Double
Floating-point numbers are numbers with a fractional part, such as3.14159,0.1and-273.15。
Floating-point types can represent a larger range than integer types, and can store numbers larger or smaller thanIntthe type. Swift provides two signed floating-point types:
- DoubleRepresents a 64-bit floating-point number. Use this type when you need to store very large or highly precise floating-point numbers.
- FloatRepresents a 32-bit floating-point number. You can use this type if high precision is not required.
Note:
Doublehas very high precision, with at least 15 digits, whileFloathas only 6 digits at minimum. Which type you choose depends on the range of values your code needs to handle.
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 collection of character sequences, for example:
"Hello, World!"
Character: Character
A character refers to a single letter, for example:
"C"
Optional Type: Optional
Use optional types to handle situations where a value may be missing. An optional type represents a value or no value.Numeric Ranges
The following table shows the memory storage space for different variable types, and the maximum and minimum values of the variable types:
| Type | Size (bytes) | Value range |
|---|---|---|
| Int8 | 1 byte | -128 to 127 |
| UInt8 | 1 byte | 0 to 255 |
| Int32 | 4 bytes | -2147483648 to 2147483647 |
| UInt32 | 4 bytes | 0 to 4294967295 |
| Int64 | 8 bytes | -9223372036854775808 to 9223372036854775807 |
| UInt64 | 8 bytes | 0 to 18446744073709551615 |
| Float | 4 bytes | 1.2E-38 to 3.4E+38 (~6 digits) |
| Double | 8 bytes | 2.3E-308 to 1.7E+308 (~15 digits) |
Type Aliases
A type alias defines another name for an existing type. Type aliases are defined using the typealias keyword. The syntax is as follows:
typealias newname = type
For example, the following defines a type alias for Int named Feet:
typealias Feet = Int
Now, we can define variables using the alias:
import Cocoa typealias Feet = Int var distance: Feet = 100 print(distance)
We use playground to run the above program, and the output is:
100
Type Safety
Swift is a type-safe language.
Because Swift is type-safe, it performs type checks when compiling your code and flags mismatched types as errors. This allows you to discover and fix errors 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 a 'String' string cannot be assigned to an 'Int' variable.
Type Inference
When you need to deal with different types of values, type checking can help you avoid errors. 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 explicitly specify the type, Swift uses type inference to choose an appropriate type.
For example, if you assign 42 to a new constant without specifying a type, Swift can infer that the constant type is Int, because the initial value you assigned it looks like an integer:
let meaningOfLife = 42 // meaningOfLife 会被推测为 Int 类型
Similarly, if you don't 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 floating-point numbers, Swift always chooses Double rather than Float.
If both integers and floating-point numbers appear in an expression, it will be inferred as Double type:
let anotherPi = 3 + 0.14159 // anotherPi 会被推测为 Double 类型
The original value 3 has no explicit type declaration, and a floating-point literal appears in the expression, so the expression is inferred as Double type.
Examples
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 is:
42 3.14159 3.14159Other Extensions