Swift Data Types
When programming in any 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 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 that determines what kind of data they can store.
Built-in Data Types
Swift provides a rich set of data types, some of the commonly used ones are listed below:
Int
Generally, you do not need to specify the length of an integer. Swift provides a special integer type Int
whose length is the same as the native word size of the current platform:
- On a 32-bit platform,
Int
is the same size asInt32
. - On a 64-bit platform,
Int
is the same size asInt64
.
Unless you need a specific size of integer, it is usually sufficient to use Int
. This enhances code consistency and reusability. Even on a 32-bit platform, Int
can store integers ranging from -2,147,483,648
to 2,147,483,647
, which is usually large enough for most purposes.
UInt
Swift also provides a special unsigned type UInt
whose length is the same as the native word size of the current platform:
- On a 32-bit platform,
UInt
is the same size asUInt32
. - On a 64-bit platform,
UInt
is the same size asUInt64
.
Note: Avoid using UInt
unless you specifically need to store an unsigned integer that matches the native word size of the current platform. In other cases, prefer using Int
even if the values you need to store are known to be non-negative. Using Int
consistently enhances code reusability, avoids conversions between different numeric types, and matches type inference for numbers.
Some considerations for integer types:
- On a 32-bit system,
Int
is the same size asInt32
. - On a 64-bit system,
Int
is the same size asInt64
. - On a 32-bit system,
UInt
is the same size asUInt32
. - On a 64-bit system,
UInt
is the same size asUInt64
. Int8
,Int16
,Int32
,Int64
represent 8-bit, 16-bit, 32-bit, and 64-bit signed integer forms, respectively.UInt8
,UInt16
,UInt32
,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 component, such as 3.14159
, 0.1
, and -273.15
.
Floating-point types can represent a wider range of values than integer types and can store numbers that are larger or smaller than those that can be stored in Int
. Swift provides two signed floating-point number types:
- Double represents a 64-bit floating-point number. Use this type when you need to store large or highly precise floating-point numbers.
- Float represents a 32-bit floating-point number. Use this type if precision requirements are not high.
Note: Double
has a high precision, with at least 15 decimal digits, whereas Float
has a minimum of 6 decimal digits. The choice between these types depends on the range of values your code needs to handle.
Boolean: Bool
Swift has a basic Boolean type called Bool
. Boolean values are logical, as they can only be true or false. Swift has two Boolean constants, true
and false
.
String
A string is a sequence of characters, for example:
"Hello, World!"
Character
A character refers to a single letter, for example:
"C"
Optional: Optional
Numeric Ranges
The table below shows the storage space for different variable types and their respective minimum and maximum values:
Type | Size (bytes) | 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
Type aliases define an alternative 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 an alias Feet
for the type Int
:
typealias Feet = Int
Now, you can define variables using the alias:
import Cocoa
We use a playground to execute 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 any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.
import Cocoa
var varA = 42
varA = "This is hello"
print(varA)
The above program will result in an error in Xcode:
error: cannot assign value of type 'String' to type 'Int'
varA = "This is hello"
This means that a 'String' cannot be assigned to an 'Int' variable.
Type Inference
Type checks help you avoid errors when you're dealing with values of different types. However, this doesn't mean you have to specify the type of every constant and variable explicitly.
If you do not specify the type, Swift uses type inference to figure out the appropriate type.
For example, if you assign a new constant the value 42 without specifying a type, Swift infers that the constant is of type Int, because the initial value you assigned looks like an integer:
let meaningOfLife = 42
// meaningOfLife is inferred to be of type Int
Similarly, if you don't specify a type for a floating-point literal, Swift infers that you want a Double:
let pi = 3.14159
// pi is inferred to be of type Double
When inferring the type of floating-point numbers, Swift always chooses Double instead of Float.
If an expression contains both integers and floating-point numbers, it will be inferred to be of type Double:
let anotherPi = 3 + 0.14159
// anotherPi is inferred to be of type Double
The integer 3 does not have an explicit type, but because there is a floating-point literal in the expression, the entire expression is inferred to be of type Double.
Example
import Cocoa
// varA is inferred to be of type Int
var varA = 42
print(varA)
// varB is inferred to be of type Double
var varB = 3.14159
print(varB)
// varC is also inferred to be of type Double
var varC = 3 + 0.14159
print(varC)
Executing the above code will produce the following output:
42
3.14159
3.14159