• 20 hours
  • Medium

Free online content available in this course.



Got it!

Last updated on 4/27/23

One more thing about variables

We've got to learn one more thing about variables...

Wait a minute! Don't we know it all already??? :diable:

We do know almost everything! Now let’s complicate things a little bit and talk about TYPES of types.

There are two :ange::

  • Value types

  • Reference types

Value types

A value type implies that a variable associates directly with some data (or value). Variables of value types always keep a unique copy of that data. 

Value type in action

For example, if we want to create a scorecard with #10 on it, we can use an integer variable:

var firstScorecard = 10

Then we want to create another scorecard as a replica of the first one:

var secondScorecard = firstScorecard

And finally, we want to adjust the value of the first one:

firstScorecard = 8

Now let's see it all together and observe the state of two variables along the way:

var firstScorecard = 10 // create the first card and assign 10 to it
var secondScorecard = firstScorecard // create the second card as a replica of the first
// observe the values
print(firstScorecard) // 10
print(secondScorecard) // 10
firstScorecard = 8 // adjust the value of the first card
// observe the values
print(firstScorecard) // 8
print(secondScorecard) // 10

It's extremely important to note that the value of the secondScorecard remained 10 - the same value it had when we assigned it using secondScorecard variable.

Once again, the line of code  var secondScorecard = firstScorecard    made a copy of the value 10 and kept it no matter what we did to the original firstScorecard variable.

This is how value type variables work, they always keep a copy of what’s given to them.

And this behaviour is what we've assumed for variables so far.

Which types are value types? 

Value types are simple types, like the numeric types we know (Int, Float, Double) as well as Boolean, String, Array, Dictionary and a few more sophisticated ones which you will learn later :ange:.

What about reference types?

Let's look at the second category in detail.

Reference types

A reference type is only a reference to the data. One or more variables of a reference type share a single copy of data. 

In the case of reference types, the actual value of a variable is a reference , or in other words, the address of its data in the memory.

That reference points to its data. For that matter, variables of reference types are also called pointers.

Reference type in action

Imagine your friend Joe found out about ancient treasures buried in Africa:


and he's got the exact coordinates:


Joe shared this knowledge with you. Now you possess the same information, pointing to the exact same spot in Africa.

A Russian spy Agent01 overheard your conversation and now knows all about the treasure and its coordinates.

All three of you can now refer to the very same treasure.

How do we code all this o_O?

Thankfully, we have a handy subject in this course - objects!  So, here we go:

// treasures
class Treasures {
var lotsOfGold = 5000 // FYI: Measures in tons
// knowledge acquisition
var joesKnowledge = Treasures()
var yourKnowledge = joesKnowledge
var agents01Knowledge = joesKnowledge

And let's visualize:

Joe & YOU vs. Agent01
Joe & YOU vs. Agent01

Next, if the spy gets there first and takes half of the treasure, both you and Joe are affected. You still know the coordinates but you will not find what's expected there - only half of it:

// treasures
class Treasures {
var lotsOfGold = 5000 // FYI: Measures in tons
func removeHalf() {
lotsOfGold /= 2
// knowledge acquisition
var joesKnowledge = Treasures()
var yourKnowledge = joesKnowledge
var agents01Knowledge = joesKnowledge
// observe the data
print(joesKnowledge.lotsOfGold) // 5K
print(yourKnowledge.lotsOfGold) // 5K
print(agents01Knowledge.lotsOfGold) // 5K
//remove half
// observe the data
print(joesKnowledge.lotsOfGold) // 2.5K
print(yourKnowledge.lotsOfGold) // 2.5K
print(agents01Knowledge.lotsOfGold) // 2.5K


 This is how reference types work - they always share!

So, in contrast to value types, reference types do not make a copy of data (the treasure), they only store the reference to a single copy of that data (the coordinates).

Amnesia a.k.a Memory Leaks

Let’s make it a bit more complicated just for fun.

What happens if all 3 of you forget the coordinates? Well, all the references are lost. That’s called a memory leak! It occurs when the data is still present in memory but no variable is pointing to it.

This is a very dangerous situation, however, the good news is that most of the time we don't have to worry about it because it’s all managed ‘automatically’!

Swift implements what’s called automatic reference counting (or ARC for short). Every time we create a variable to reference some data, the counter gets incremented. Every time a variable that points to that data gets dismissed or reassigned to point elsewhere, the counter gets decremented. And as you may now guess, when the counter becomes zero, the memory that was occupied by that data become available for other purposes.

Which types are reference types?

As you can tell from the example we used here, classes are reference types! 

And when is a copy of data created in memory? 

A copy of data is created in memory when we create an instance of a class. And if we assign an object variable to another variable, the new variable will point to exact same object as the first variable:

var joesKnowledge = Treasures() // an object is created in memory, a reference to that data is assigned to joesKnowledge variable
var yourKnowledge = joesKnowledge // only a reference to that data is assigned to yourKnowledge variable
var agents01Knowledge = joesKnowledge // only a reference to that data is assigned to agents01Knowledge variable

Now, what if I dismiss the first (the original) variable, will the second one still point to that data? yes, it will still point to the same data and the data will remain unchanged. It’s like if Joe, who originally shared the coordinates of the treasure with you, forgot the coordinates. Not a big deal - you still remember it and the treasure is still there :zorro:!

Direct comparison

There’s a important trap we must be aware of with this concept: the direct comparison of variables.

If we were to compare value type variables, say, two integers for our scorecards, the comparison is applied to the numbers themselves, so we compare 10 with 8.

When we compare reference type variables, only the references get compared.

For example if you know coordinates of a treasure and your friend Livia knows coordinates of a treasure, you can compare your variables, but the only thing you can establish is whether you are talking about the EXACT same location.

Alternatively if coordinates are different you may or may not be referring to the same value of the treasure, even if both locations store 5 tons of gold!

That’s exciting, isn’t it ;)?

And what's the relation of this to NeXTDestination?

You noticed that we have sets of destinations and entertainment options available to plan our journey. As we chose them, we will be collecting them in our accomplishments and placesVisited. We will need to be mindful of the fact that those initial collections (destinations of the adventure class and entertainment options of the destination class) will be shared instances of data!

Let's Recap!

  • There are two categories of variable types:

    • Value types - each variable associates directly with its value in the memory.

    • Reference types - each variable points to its value in the memory, i.e. stores the memory address of its value. 

  • Direct comparison of variables of value types compares the values themselves.

  • Direct comparison of variables of reference types compares the pointers/addresses in the memory but NOT the values themselves (!).

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement