Properties are one of the fundamental building blocks of both classes and structures. In this lesson you will be introduced to the primary types of properties, both stored and computed.
1.Introduction4 lessons, 26:24
2.Language Constructs12 lessons, 1:51:34
3.Swift and Object Oriented Programming5 lessons, 27:52
4.Built-In Types5 lessons, 42:18
5.Conclusion1 lesson, 01:19
The first topic that we're going to begin discussing, when it comes to the concept of classes as well as structures, is the ability to create properties. Now, what exactly are properties? Well, properties are nothing more than variables or constants within a class or a structure that are used to hold on to some piece of data. So there's no functionality to them necessarily. Although you can add some functionality to them as we'll see in a little bit. It's basically a way to define within your class a way to store data. So let's see what that looks like. So we're gonna create a class, and in this case, we'll call it calculator. And we, as you can see here, we can create properties and methods and I wanna create a very basic property, just a stored property where I'm gonna put some data in there that I can retrieve later on. So the way that you do that, like I said, is by creating either a variable or a constant, depending on what you're looking to do. So in the case of a calculator, if I wanted to create, say, a total property that's going to contain the running total of all the operations that I'm doing on my calculator. So if you're familiar with using a very simple calculator, I can add two numbers together. I can say 2 plus 2 equals 4. Now, the total in that case will be four and then if I were to hit plus two again, then it's going to increment that total by two. So that's the, and it would turn into six. So that's the basic concept I'm trying to start to build out here. So in this case we're going to say var and we'll call this total. And once again this is just a variable. So I can specify this is as being an integer, and I can even initialize it to be zero. Something as simple as that. So now I have a very basic stored property where I can add some value to it. I can put some data into this particular property and retrieve it later on. So I could do something like var myCalc so I now have a way to store a new instance of my calculator, and by default you're given the default initializer within your particular class which is just going to be equal to, it's going to look like a function call to your actual class. So we can say calculator, open close parenthesis. Because we're just using the default, there's no parameters here, but we'll talk about that a little later. We now have a new instance of the calculator class stored within myCalc, and once again, I could be explicit about this and say that this is going to be of type calculator. And that will be absolutely fine as well. But in this case, we'll just use it implicitly. And now, because I have this property defined here, this total property, I can say myCalc, and I can ask for what the total is. And as you can see at the total here is zero. Now, I could also set that, I could say my calculator.total is going to be equal to 8 or something along those lines. So now, you can see that myCalc total is going to be 8. So now I have access to be able to set that and retrieve it just with anything that I might want. Now, as you can probably imagine, that's not what we're gonna want to be able to do. We don't want people from the outside of our application to be able to modify the total. Explicitly, in this way we want the only modification of that total property to be coming internally from our application but we'll show you how to do that in just a few moments. Another concept is to be able to to create a constant within our class. Now, if you do recall, creating a constant is very similar than, or it's very similar to creating a variable, but you're gonna use the let keyword instead. So if I say let, and I can now define a constant. And, what's a typical constant you might find within a calculator? Well, the first one that comes to my mind would be pi. So, I could say something like, I would let pi be equal to, we will just say 3.14. Now, you can get as explicit as possible, but what we're basically trying to show here is that pi is not a value that's going to change. Now, I can make this out to more precision, that would be fine, but in our case this should be just fine, so that value is never going to change and I don't want somebody to be able to come into my calculator and say oh well, I am deciding now that pi is actually going to be equal to 5 or something like that. So that's not gonna be able to work because this is, of course, a constant and I cannot assign or change the value of a constant. So that will prove to be very useful later on as you start to maybe create more functionality around values that shouldn't need to change. So now we have a basic constant that can't be changed and a stored property where we can put some data in there and we can adjust it however we deem necessary. So this is just using that basic functionality of being able to assign data to a particular variable. So what if we wanted to change the way that that's actually getting data in or getting data out? We wanted to compute something to actually get the value of what is being stored in total. Well, the way that we do that is by using what's called a computed property. So let's get rid of these for now. And let's adjust this particular property right here. So we start by doing this the same way. We say var total is going to be equal to an integer. Instead of defining it or initializing it to have a value, we're gonna specify the open and closed parentheses, which is just going to let us create some functionality within here. So now a computed property has two specialized ways to access data, and that's via a getter and a setter. Now these are just fancy keywords here, but ultimately that means you have a get, so I can define a get for this total, or I can put in some functionality in here, which is basically going to encapsulate as much as I want, I can put as much calculation logic in here that I might want and then I also can define a set. Now, a set is gonna do the same thing and you can almost think of these as methods where if I'm going to set this I can set it to a particular value and I can give that value a name, so I could say maybe newTotal, something along those lines. So now I have access to a variable here called newTotal that's going to allow me to put some logic around what I'm trying to set this to. So one thing that is happening here is you can see I have a little error here, and this is going to say that I can't just create a getter and not return a value. So because I defined my total variable, my total property to be an integer, I have to return an integer out of this particular getter. Well, let's just create a new property here. We'll call this var runningTotal. And this is also going to be an integer. And we'll define this to be zero. And maybe this is gonna be the one that's going to contain the running total of all the operations that we have going on within our current context of our calculator. Well in this case, we're going to simply return our running total. And that will absolutely be fine, so now we're returning an integer here, now this may seem a little redundant, and I'm only showing this for illustrative purposes, but you could have all sorts of crazy logic going on in here, but in our case, since we're just doing a very simple calculator, this will probably be fine. Now, when we're actually going to set it, we can set our particular value or whatever it is we're looking to store within our class to this new total that's being passed in. And because this is an integer type, this total is an integer type, it is assumed or inferred that this new total value is also going to be an integer. So in our case, if we're going to set the total, we don't really want to set the total because this is just kind of the access into the world of the calculator to get a total. What we wanna set is our running total equal to whatever this new total value is. So, how does this work now? Well, it works the same way that we have defined before. I now can create a new instance of my calculator. So I'll say, myCalc is gonna be intra, in, set to calculator, but if you look in here, you're not gonna see like a getter and a setter. You're still just going to see the pi constant and we can also see running total and total amount. So the total, every time we access it this way where we are trying to retrieve the value out, it's going to execute this getter logic here, which in our case, is only returning running total that's why we're getting a zero. And if we set this value then we're going to see that running total is actually going to increase as well, so let's see how that works. So, we'll say myCalc.total is gonna be equal to 8. So if I do that, I can now look into myCalc and you're gonna see that pi is still 3.14, but running total also got set to 8, and that's because within our setter, that new total value was assigned to our running total property. Well, this is where we start to get into the ability to limit what the outside world can do with our particular properties or functionality. And we can start to introduce the concept of a read-only property. So lets say that in our case, we don't want people to explicitly be able to set total. That is not a piece of functionality we want to expose. So in order to restrict them from being able to actually set this data, I'm going to get rid of this setter. So now you're gonna see, as soon as I got rid of that setter, I now get an error here when I'm trying to assign a value to total, and you're gonna see that I cannot assign to total in myCalc, so that is no longer allowed. So, as you can see, I only have my getter. Now, you can leave it this way if you would like, but since we're only doing a getter here and not a setter, I don't have to explicitly say that this is a getter. It can be assumed that what's happening here, because I am going to return a value, that this is going to be the getter. So now I can once, I can still get the value and it's still going to execute this getter functionality, but I still cannot set this to any sort of value. So that's kinda nice. I'm now restricting access to certain properties by making them either read only, by limiting what is able to be done via the getter or the setter. So as you can see now, with just being able to create some basic stored and calculated properties, we're able to contain some data within our classes or within our custom types. But this is a little limiting. We're only being able to do very simple things by storing data where, sometimes we're gong to need to be able to do operations on that data and provide some functionality around it and that is where the methods come into play. And that's exactly what we're going to do in the next lesson.