iOS Development #4: Variables

iOS Development #4: Variables
Jan Zavrel
Jan Zavrel Follow June 23, 2021
Share:

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:

  1. 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.
  2. 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.
  3. 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 104.198.14.52 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:

var age: Int = 40

In this short code, we did this:

  1. We declared a variable (var)
  2. We gave it a name (age)
  3. We specified a data type (Int)
  4. 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:

print(age)

Go ahead and run your code:

Age printed to the console

Follow me on Medium, Twitter, Facebook and YouTube.

Share:
Jan Zavrel
Written by

Jan Zavrel Follow

Developer, Author, Teacher, Evernote Certified Consultant.