Unlike our simple program that’s only able to print bunch of text to the console (in case it’s not all commented out), real programs need to produce some output based on the input they get from the user.
This user input, typically some data, has to be stored somewhere in the computer’s memory so the computer can work with it. That’s why every single programming language works with variables and Swift is no different in this regard.
Computer stores data in memory cells. Each cell has its own address so the computer can access it to either store or retrieve values from it. However, the address of the memory cell is quite hard to remember for humans because it looks like this: 0x6dfed4
Let’s say you want to store your age to the computer’s memory:
First, you need to tell the computer to reserve a portion of available memory for your age. This is called memory allocation and it’s done by a declaration which tells the compiler where and how much storage to reserve.
Next, you need to specify what type of data you want to store in that reserved portion of memory. This is important because based on the data type, the operating system allocates memory and decides what can be stored in there.
Finally, you need to specify whether you will change the stored value in the future or not. Swift offers to kinds of storage for this reason, variables, and constants.
Variables (and constants) are named containers that store values in memory. If you’re familiar with domain names, this works in a similar manner. You can always access this website by directly typing its IP address 220.127.116.11 to your browser, but it’s easier to type codewithjan.com which resolves to the corresponding IP address automatically.
It’s actually much more complicated, and network specialists would disagree with me, but I just wanted to make the point here.
There’s a simple difference between variables and constant that you need to remember. Unlike variable, which value can vary, in case of constant, the value you store initially can’t be changed later.
If you want to store your age, it’s better to use a variable because your age changes every year. On the other hand, if you want to store your date of birth, you can use a constant instead as this date stays the same forever.
Every variable (and constant) has a type, which represents the kind of information you can store inside of it. This tells the computer how much memory to allocate for your data, and it also defines what you can do with the stored values.
Here are some of the basic data types:
Int: integer numbers
Double: floating-point numbers
String: a sequence of characters
Bool: true/false values
Ok, let’s test this in practice and store your current age. Delete everything you wrote so far and type this line of code:
In this short code, we did this:
We declared a variable (var)
We gave it a name (age)
We specified a data type (Int)
We initialized the variable with the value (40)
Now, when you have your age stored in your variable. You can print it to the console with a familiar print() command: