1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Advanced Techniques in Swift

Reference vs Value Type

Contents

keyboard_tab
Advanced Swift
2
Struct Overview
PREVIEW6m 10s
6
Tuples
7m 23s
7
Guard Let
5m 52s

The course is part of this learning path

Start course
Overview
Difficulty
Advanced
Duration
51m
Students
3
Description

This course focuses on advanced techniques in Swift. You'll be introduced to structs and how we can use them in our code.

Transcript

Hi, within this lecture, we're going to see the difference between reference type and value type. But before we go on and do that, we have to understand the immutability part as well. As I've said before, structs actually provide us the opportunity to work with true immutability. So, what do I mean? So, I created this class with let, and I created this struct with let. So, I created both of them with constants, right? Can I come over here and say classJames.age = 51. And can I print it? Let's try that, okay? So, first, I get this, okay? So, let me comment this out. I know of the initial value of this 50. Can I get 51 in here? Yes, I can. So, we can do that and we have seen we can do that before. But can I do that with structJames? Let's try to give 51 here and let's try to print this. StructJames.age. Let's run this. I couldn't even run it because as you can see, it says that structJames is a let constant. So, why does it happen? How could I change the age of this classJames, but how couldn't I change this structJames's age? So, if I come over to class, I will see that this age is a variable that's why I could have changed it. But it's variable inside of struct as well. So, as you can see, we don't expect this to be happening. But in order to change the attributes of a struct, you have to declare it as a variable in the first place. So, if I do this, if I change it to var, then this error will go away. And if I run it, I will see 51, 51. So, this gives us both options. If we want to have a true immutability, which means that if I just define it as a constant and I don't get to change the attributes as well. But if I want to change the attributes later on, I have to define it as a variable. So, we have both options in the struct. In class, even though I do it with let, it lets me change the attribute. So, this is for mutability and immutability part, okay? Immutable struct. And now we're going to focus on reference and value types. So, remember classes are reference types whereas the structs are value types. And best way to understand the behavior difference between them is to copy an object. So, we're going to copy the structJames and we're going to also copy the classJames as well. So, let me create another variable called copyOfClassJames, okay? And I'm going to make this equal to classJames. And I'm going to do the same thing for struct as well because I'm going to compare the behavior differences between them. So, this is going to be structJames. So, I'm basically copying this and I have to define this as var in order to overcome this immutability problem, okay? And I can define the copy of class as var as well, but I'm not going to deal with those because it's already mutable. So, if I print the copy of classJames.age and if I print copy of structJames.age, what will happen? Of course, we're going to get 51 again, right? As you can see, we're getting 51 because we have changed them to be 51. Now, can I just change the age of our copy structs or copy class? Yes, of course. Can I come over here and say copyOfClassJames.age = 52? And can I do that with struct as well? Yes, because I declared it as a variable, right? So, I can do that. So, let me print those ages one more time and see if we have any different behavior than we expect. Now let's run this, and here you go. We have 51, 51, 52, 52. So, there is no unexpected thing going on here. So, where is the surprise? The surprise will come if you print the classJames.age and class as structJames.age. So, did we change the age of the classJames and did we change the age of the structJames? No, we didn't. So, we expect to see 51, 51 when we print this, right? So, we're going to be seeing 51, 51, 52, 52, and then 51, 51. So, that's what my mind tells me. But if I run this, I'm going to get a very weird result, so I'm getting 51, 51. Yes, that's good; 52, 52, but I'm getting 52 as the classJames.age. I didn't even change it. Why does this happen? So, this is happening because classes are actually reference types. So, when I create a new class, new object out of the same object, it doesn't create a new object for me. It just creates a new reference to the heap to find that object. Whereas, in the structs, we don't work with heap, we work with stack. So, in the stack, we create a new value, new object. So, when I change the copy of classJames age, it actually changes the classJames age as well. So, reference types are associated with classes, and this is what happens when I copy an object. I copy the object and it just creates a new reference rather than creating a new object. So, same object with new reference pointing to the heap to find that object. So, we have multiple references with a one object in this case. We can't do that for multiple occasions, so we will end up with a lot of references trying to work on the same object. It's kind of dangerous because we might lose track from some point on. But however, in structs, we don't have that, right? So, in here we have one object and we have two references. On the other hand, when we work with value types and it's associated with the struct, when we say copy, it goes and creates a new object. So, new object, rather than creating a new reference. So, we end up with two objects, so that when we change the age of the new copycat, we won't change the age of the previous struct object. So, this is very important. In struct, we deal with value types. In classes, we deal with reference types. And the logical way of thinking is to work with structs because we create a new value, and Apple actually suggests that. As I said before, they say that you have to work with structs as much as possible. And if you're really looking forward to work with the reference type or if you have to work with an inheritance type, then you're going to go to classes. But again, then you will have to be careful because if you create more than one reference, you can mess up with some values at some certain point without even noticing it. So, that's it for the major difference between the struct and the class. We're going to have to see something called mutating function in order to understand this completely. That's what we're going to do within the next lecture.

 

About the Author
Students
420
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics