7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 Variables and Constants

To get started with most programming languages, it pays to learn how to allocate memory for storing different types of data. In other words, how can we create different types of variables and constants? That is exactly what we are going to learn in this lesson.

Related Links

2.1 Variables and Constants

In this section we're gonna spend some time going through the different language concepts and constructs that you're gonna be using quite consistently throughout you software development career in general, not just in the Swift programming language. So I'm talking about all sorts of different concepts here, and we're gonna start with the basics of creating some storage space within your application to be able to hold on to values or some sort of data. Now, if you hear those words you're immediately going to start thinking about things like variables and constants. And that's exactly what we're getting at here. So when it comes to creating some variables we have a couple of different options. So, the basic is always gonna be starting out with the var key word. Now, var is kind of that visual tip that you're going to be creating a variable here. And by variable I'm talking about some storage space within your application that you can change. It's modifiable, or it's mutable. You can change the value that is within this variable. So what's gonna happen here is I can create a variable. Let's say we're gonna call it stuff. And when it comes to naming conventions, you're basically free to do whatever you want, with a couple minor rules. You cannot use any reserved words. So I can't create a variable called var, that just isn't going to work. So I'm gonna have to use you know, basically non reserved alpha numeric characters, you can't use white space or special characters, or mathematical symbols or anything like that. So if you basically keep it to alpha numeric characters you're gonna be just fine. So now at this point I have an option. So if I'm gonna create a variable, odds are I'm going to initialize it to something. I don't have to, but odds are I'm going to do that. So let's say, if at this point I know that the stuff variable is going to contain say a string. I have a choice to either explicitly state to Swift and to the compiler what the type of this variable is going to be. So if I want to do that, I specify a colon directly after the name, give it a space, and then type out the type. So in this case, I'm specifying explicitly that the stuff variable is going to be a string. So at this point, I can leave it here uninitialized, but be careful at this point. So if any point down after this part of the code where I'm actually declaring this, since I haven't initialized it, I can't use this, I can't say stuff is empty. If I do this, I'm going to get an error because it already knows that I did not initialize the stuff variable. So you can definitely do this if you don't know what the value is going to be as you are creating this variable, maybe you're gonna do some logic and that logic is gonna dictate what it is. But it's just one of those things you need to be aware of, so that you don't make any errors down the road, because you didn't initialize it. So lets see how we can initialize this. So it's quite simple, you're just going to set it equal to some string value and that string value could be an empty string, or you could fill it up with some data. We could say, this is, has data in it. And then after that, subsequently, because we did initialize it, we can start to use some of these properties and methods of the string class or of the string type that will allow us to get some additional data from this particular variable. So in this case I'm checking to see if the stuff variable is empty, and it truly is not. So it's kind of a nice convention. Now another thing that you can do or another option you have if you know, at the time of declaring the variable, that you are going to be initializing it with some sort of data, you have the ability to not have to explicitly tell Swift what the type of the variable is going to be. You can let it infer what the type is, so I could get rid of this string and this colon and just leave it like this. Now this is still valid Swift, this will compile and run just fine, and what's happening here is that Swift and the compiler are figuring out based on the declaration and the subsequent initialization of this variable what the type of the variable will be. So since we are initializing it at the same time, it can figure out what the type of this variable is going to be, and ultimately be a string. Now, if you wanted to create several variables, you could obviously come down here and continue to do var more is gonna be equal to maybe more or, and just keep going down the line this way. But you don't have to do it this way, you can definitely stack these together so that you're only creating wanted it, so you're only creating one line and putting these things all together. So I could say stuff is equal to data. I could say more is equal to more, and I can continue to create them all down the line here just I would like to without any problems. And you'll see here that we're not gonna get each of those individual ones. You're gonna see that this was run two times or that there was two initialization steps. And then I can come down here and I could see what's the value of stuff. What's the value of more? And everything just continues to work. So that's a nice little convention to save yourself some vertical space if you'd like to put them together horizontally. Now this is the process you're going to use when you're creating variables. Now on the other side of that, we have the concept of a constant. Now a constant is very similar to a variable in the fact that we are going to create some storage space within our application to store some information, or some data. But the difference is once you've created a constant, you cannot change the value. So at this point I can say all right, well stuff is no longer gonna be equal to data. It's going to be equal to stuff. And then I could say more is now going to be equal to more stuff. And if I were to come down here, I can query now what stuff and what more values store, and as you can see here, everything continues to work. But if I wanted to create a constant or something that I know isn't going to change, we're gonna do the exact same thing that we've done previously when we're creating these variables with one minor change. Instead of using the var keyword, we're going to use the let keyword. Now the let is what the vis, the visual key is going to be that we're talking about a constant here. So in this case, I'm gonna say that this is sumConstant and at this point I can do the same thing. I can maybe say that this is gonna be an integer and it's gonna be set to 2000. So at this point, this is a completely valid line in Swift. I've created a constant of integer type that's set to 2000. Now, obviously, I don't have to explicitly state this here. I could leave it just this way without the type and it will infer what the type of this variable is. But the difference is, if I were to come in here and say, all right, I want to now take some constant and I want to set it to 1000. We're gonna see that this is gonna cause a problem, because we cannot assign a value to a constant because it was declared with the let keyword. So that's really the only difference there when you're creating these constants versus variables, we're talking about using the var keyword versus the let keyword. And you've also noticed herein, whether it was immediately obvious to you or not, the Swift programming language does not require you to use semicolons. Now, you absolutely could put a semicolon here, and continue on your merry way, but it's not required. So it's going to use the line breaks as the implicit representation of the ending of a line. Now, you could put things together on the same line this way. This is valid Swift, you can put these semicolons in here but odds are, if you're going to put things on one line you're probably not gonna separate them out with semicolons unless that's just what you're used to doing. So there you go, we have the process of being able to create variables, to create constants and we've kinda pointed out the fact that the Swift programming language does not necessarily need to use semicolons to separate lines.

Back to the top