[MUSIC] All right guys, we're back, and one of the things that we have to decide is to how much detail we actually go regarding every aspect of swift, we obviously can't discuss everything so the idea is that some basic things. We go over and then the more advanced topics, if you're interested or when needed, you can find yourself. So, we've talked in this module for about an hour so far about the different topics like properties, and closures, and optionals. There's one more item that we believe, and Jack strongly believes, that's very important that we do need to discuss of some depths. But, then after that, what we're going to do is provide essentially a very quick summary cheat sheet of what are some of the advanced topics that you should be aware of. And, if anything is of interest, then we'll cover it maybe to a certain extent, but then we'll leave it up to you to find out more information by yourself. But, before we get to that, we're going to talk about. >> Value types. >> Value types. >> Yes. So we mention this a bit, up to now in the course already. There is a difference, up to now we've talked about reference semantics with inner properties and optionals. But there's also sort of value types and these are just giving names to things you already know if you've done programming. So say you have an integer, and it's equal to three, and then you assign it to b, and then you change b. Let's I guess make them all variables, and the change equal to five. So what do you expect a to be?. It would be 3, right? In any language, doing this with sort of a integer, it would be really weird if a also changed to 5. But it wouldn't be weird if there was a class. So what this is displaying here is sort of value semantics, which means what matters about a, this integer, is it's value. It doesn't matter like where it came from, or like what its origin is, its history, its story. It's just its value which is three. And when you assign it, all you have to do is copy the value. And so a is a value type with a value of three, and b is assigned to a. And when that happens, a is copied into b. And so when you assign b to something else, it doesn't change a at all. And this is a classic example of a value. And something that you should be very familiar with, because you would be very surprised if it behaved some other way. But again, let's bring back our number type, that's simply holding number. And you see where this is going? [LAUGH] Like if I create a default to zero. All right, we're actually gonna create a initializer. >> And as Jack's writing, essentially you're used to seeing >> The point of this is pointers. So if you have a pointer that is equal to a different pointer, they're actually pointing to the same object, then if you change one object, the other one is pointing to the same object, so that would also change. >> Yes, so if there was sort of a pointer syntax, that would be easier to understand. In Swift, this came with Swift is there's no pointer, right? You're not sure just by the assignment and sort of creation, you're not sure if it's a pointer or not. And as you'll see like a number, can be a number with three. And we'll copy that for b, and then make it equal to a. Let's continue with the label. And now if I set b.n is equal to five, you expect a.n to also be five. All right, it's a number. Right, so b being the number that changed but a number, did I not, also changed, and that also makes sense, it's by which you can see here that sort of the syntax here is not that much different. And the only thing causing this behavior is because this is a class, now the way we're gonna talk about this is integers are value types and Numbers are reference types, so when you're assigning it all you're doing is assigning a reference. Because for classes you don't know how much data it hold. It can hold huge amounts of data, and you make copies of it. And that's just sort of a classical programming solution is to assign this reference. And so for reference types when you assign b to a it's simply, it pointing the same object, and then when you change b.n, you're changing the same exact thing a.n. Okay, so why is this more important in Swift? It's because Swift brings out value types more front and center compared to some other languages by providing structs. So a struct is very similar to a class, except that it's a value. So we have this pretty much the same exact class. I'll make it like super exact. Literally copying in the init function. Even better. Let's just change this to a struct. Just by doing that, you'll see the behavior completely change, and this is because, all of a sudden, numbers is now a value type, because you declared it as a struct. And now it behaves sort of like integer, when you assign it, it copies the entire struct, including all its internal values. And you'll have to reassigned it, it remains three. [SOUND] So that's the main idea of that we wanted to cover today, is the difference between value types and struct types. And I guess we'll talk a little bit about their advantages, like having a, why are value types valuable? It's because you don't have to worry about where they came from, and don't have to worry about behavior. Like previously with a class. And it allows for more safer code, when dealing with data. References are passed around a lot, and many people can kinda reference to the same object. And when you modify, it can have undesired consequences. Maybe you changed someone else's value, that they were depending on, unintentionally. But when you're using a struct, you know that you have your own copy. And it's very safe to modify it. >> There may be cases you actually do want a class. If you want, the fact that when you copy it, it's passed by reference, but essentially the summary so far is that classes are copied by reference. Structs are, although in many ways exact same as class, they're copied by value. >> Yes.