In Swift, each variable/constant holds a value that represents some data type.
There are lots of data types in Swift.
As a beginner, it is important to learn these 5 basic data types first:
- Int to store whole numbers.
- String to store words.
- Double and Float to store decimal numbers.
- Bool to store the truth values (true or false)
Here are some examples of variables and constants that represent different data types:
var x: Int = 10 let pi: Double = 3.141592 var name: String = "Alice" var isCold: Bool = false
In this article, we are going to take a deeper dive into the basic data types in Swift.
You start by learning what is a data type in the first place, and how they are also present in the real world.
Then you are going to see how data types are present in the Swift programming language.
More importantly, you are going to learn how Swift knows the data type of a variable and how you can specify it as well.
If you have not read about variables and constants, please, read this article before moving on.
Disclaimer: To learn Swift, you have to repeat everything you see in this guide and in the following guides. If you only read the guides, you are not going to learn anything!
Data Types in Swift
There is a lot of data in the world.
Each piece of data represents some data type.
- A decimal number describes a fractional number. For example 3.141.
- A whole number describes a full number without decimal places, such as 100.
- A word is a group of letters, such as Hello.
- The truth can be either true or false.
These are just some examples of data types in the real world.
As you may imagine, a computer also needs to know the type of data it is dealing with. Thus, it is not a surprise that data types are present in the Swift programming language.
But why are data types important?
Based on the data type, values are stored differently on your device.
In Swift programming, data types have more formal names than real-life data types like “word” or “whole number”.
Here are the 5 most important data types in Swift:
- A “decimal number” is called a Double or a Float.
- A “whole number” is called an Int (short for Integer)
- A “word” is called a String, which is a group of characters.
- The “truth” is called Boolean.
Notice that these are not the only data types in Swift. However, these are the most important ones you need to learn first as a beginner.
For demonstration, let’s create variables that represent these data types.
Here is an example of a Double:
var x = 2.32
Here is a Float, which is essentially the same as a Double:
var y = 1.32
Here is an integer or Int:
let number = 10
Here is a String that represents a word:
let name = "Alice"
Last but not least, here is a Bool that represents the truth:
let isCold = false
But as you can see, the data type is not actually mentioned in any one of these examples.
In the next chapter, you are going to learn why and how it can be changed.
How to Specify Data Type in Swift
In Swift, every variable or constant you ever create is going to have a data type.
In the previous tutorial, you learned how to create variables and constants.
Let’s bring back an example where you create a variable x and set its value at 10.
var x = 10
The data type of variable x is an Int because it represents a whole number.
But as you can see, it is not explicitly stated anywhere.
This is because Swift is able to figure out the data type automatically.
However, you can (and in some situations should) explicitly specify the data type to the variable in question.
To specify the data type of a variable or constant in Swift, use a colon after the variable name followed by the data type.
For example, let’s specify the data type of the previously mentioned variable x to be an integer (Int):
var x: Int = 10
This line of code is exactly the same as the one where the data type was not explicitly specified.
But why would you do this if Swift knows the data type automatically?
Swift is a type-safe language.
In other words, Swift makes it easy for you to be aware of the data types being used.
So even though specifying the data type is not mandatory, it is still recommended.
Also, in some situations, you are forced to specify the data type of the variable in question. You are going to see an example later in this course.
Specifying the data type also improves the code readability.
As you gain more experience and write more complex programs, being able to instantly read the data type saves time.
Next, you are going to learn why changing the data type of a variable or constant is not possible in Swift.
Can You Change the Data Type of a Variable?
In Swift, you cannot change the data type of a variable.
For example, let’s create an integer and try to assign a string to it:
var x: Int = 10 x = "test"
This causes an error:
Cannot assign value of type 'String' to type 'Int'
Not being able to change the data type is because different data types are stored differently in memory.
If you declare a variable to store integers, it can only store integers. You can change its integer value. But you cannot change it to a string for example.
This may sound a bit limiting to you. But it has a great advantage.
When a variable is locked to store only some type of data, you know what to expect from it.
If the data could be anything, you’d have no idea what type of data the variable holds. This, in turn, makes the code more susceptible to errors and harder to read.
At this point, you understand the significance of data types in Swift. Furthermore, you understand how to explicitly specify a data type for a variable (or constant).
Next, let’s take a look at the most common data types in Swift.
Common Data Types in Swift
You already saw some examples of how to create variables and constants that represent different data types.
In this chapter, you learn more about the 5 most common data types in Swift, that is:
In Swift, String is one of the most commonly used data types. A String represents textual data.
The name “string” refers to a string of characters, which is essentially the definition of text.
Here are some examples of strings in Swift:
var name = "Alice" let DOB = "1993-08-12" var favoriteColor: String = "Yellow"
Feel free to copy-paste these strings to your Xcode Playground and change their values and names.
In this example:
- The first line creates a variable that represents the name of a person. Because the type is not specified explicitly, Swift automatically determines it is a String.
- The second line creates a constant that represents a string of someone’s birthday. Here Swift also automatically determines the data type to be a String.
- The third line creates a string variable that represents someone’s favorite color. Here the type is explicitly set to be a String.
Next, let’s talk about integers.
In Swift, a whole number is called an integer.
A data type that represents integers is called Int, which is a shorthand for the word integer.
Here is an example of creating a bunch of integers to your program:
let n = 10 let myAge: Int = 26 var yourAge: Int = 30
Feel free to change the names and values of these variables and constants in your Playground file.
Float and Double
In Swift, decimal numbers are represented by two possible data types: Float and Double.
The difference between these two types is that Double is more precise than a Float.
Thus, you can stick with Double when dealing with decimal numbers in Swift.
Here are some examples:
let pi: Double = 3.141592 var distance = 329.731 var volume = 100.0
If you do not specify the data type for a decimal number explicitly, Swift automatically makes it a Double.
Next, let’s talk about representing the truth values in Swift.
In Swift, the truth value is called a boolean value.
The truth value is represented with a data type called Bool, which is a shorthand for Boolean.
A boolean value can be either true or false.
Boolean values are an important part of programming because they are used to implement logic in your program.
For instance, here are a bunch of boolean values in Swift:
var isRunning = false var isCold = true let canTrespass: Bool = false
This completes the guide on the basic data types in Swift.
Next Chapter: Operators in Swift
In this chapter, you learned what is a data type, and what are the most common data types in Swift.
To recap, every piece of data represents some data type.
In real life, you encounter data types such as text, numbers, decimals, and so on.
These same data types are present in the Swift programming language.
There are lots of data types in Swift, but the most common ones are:
- String. Used to represent text.
- Int. Used to represent whole numbers.
- Double and Float. Used to represent decimal numbers.
- Boolean. Used to represent truth values, that is, true or false.
You are going to use these data types a lot!
Next up, you are going to learn operators in Swift. These operators can be used to perform actions on different types of data.
Next Chapter: Operators in Swift
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
- Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
- Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)