7 days of WordPress plugins, themes & templates - for free! Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 Variables and Constants

The most basic concepts in Swift, as with many other languages, are variables and constants: these allow you to store and retrieve data that can be used elsewhere in your application.

2.1 Variables and Constants

To simply set the stage I'd like to take a few moments to start talking about a foundational aspect of really working with any programming language. And it's really to be able to create variables, and constants, and be able to assign data to them, and ultimately work with them. Because that's how we're gonna start working with data throughout all of the applications that we're going to write using Swift. Now the two ways that you can go ahead and declare things and assign things is by creating variables and by creating constants. Now it's a very slight difference in the way that we are going to declare them and ultimately assign to them. But this is going to really dictate the way that these pieces of data are gonna be used throughout the rest of our applications. So let's start with variables. Now to create a variable, we simply use the var keyword,.and then we're going to specify a name. So, in this case, I'm just going to call this my variable. Now I can really start to declare this or finish declaring this, and or assigning a value to this, making this an assignment statement and a declaration statement all in one shot. So as I kind of eluded to in an earlier lesson, Swift is pretty smart in and of itself and it's able to know what type a certain variable is or a certain constant is for that matter based on a declaration statement. So it's able to infer the type of a variable. So I can say my variable is going to be equal to five, and at this point, swift is smart enough to know that if I were to go ahead and look at my variable, it sees and knows that this is going to be an integer based on just this assignment statement, and we didn't have to explicitly tell it what it was. Now we can be explicit In the way that we explicitly declare a variable is to use our keyword, give it a name, and then after the name we're gonna specify a colon, and we're gonna give it a type. In this case I want this to be an int and as you see here I have a number of built in types. Ints are some of obviously the more used but there's also doubles floats, booleans, and strings and we'll talk about those a little bit later. So right now I can do this statement right here and everything will continue to work. And once again, Swift is gonna know that this is an integer. But I don't have to assign to it right away, either. I can just leave it as an unassigned variable. At this point, I have my variable is equal to, well, nothing at this point because we haven't done an assignment statement. But at least we know it's an integer so I can say all right, my variable, now you might say that that could be kind of dangerous. Because what if somebody tries to actually do something with my variable before it's actually assigned? That's obviously going to be causing some errors and ultimately crashing your application. Well, that's definitely true, but one nice thing about this is that Swift is very defensive for you. So if I were to create another variable called hash, and I wanted to set that equal to myvariable.hash value, if I wanted to do something like that, I couldn't even build this application at this point because Swift is not going to let me. Because it already knows that my variable has not been initialized yet and so we can't use it. So it's going to kind of protect you from yourself a bit there. So if I were to change this to say this is equal to ten, I could say this and go ahead and build a now that's going to work. So I could get the hash value and I could come down and maybe set my variable now equal to 50. And then I could set hash again equal to my variable dot hash value and I could do something like print. And I could print out the hash value. So I could save and go ahead and run that. And then you'll see that down the bottom of the screen the output here in the little console is going to be 50. So that's all pretty nice this is the basic functionality of being able to create a variable. Either of an inferred type or explicitly given a type. Assign some value to it. Run some sort of operation against it or get the hash value. So this also is telling you that not only can I work with these variables but a lot of the built-in types in this case, integer as well as other, unsigned integer and as I mentioned before, doubles and floats and booleans and strings all are first class objects or types really. They all have certain operations and functions and properties and fields attached to them that you can use right out of the box that you don't actually have to use. And the other nice thing about that as we'll see later on as we can actually extend the functionality of these types later on. But before I get too far ahead of myself, let's kinda reel it back in a little bit. So now I see that I can do these basic operations with a variable. And one of the defining characteristics of a variable is the fact that I can create it. And I can give it a value. And later on I can change it Hence the name Variable. It is variable. I can make changes to it. It's mutable. Now the difference between the variable and the constant is that particular feature. Otherwise, there really aren't too many differences. So a constant is just that. I can create my constant. And then I can assign to it, but after that I can't make any changes to its value. It is set in stone for the duration of your application. And the way that we create a constant is by using the let keywords. That's going to be that defining characteristic of what's going to allow you to quickly understand that this is a constant. So I could call this one my constant. And I can do the same exact things as I did before. I could set the SQL equal to five. And I could have swift infer the type. I could also be explicit about it once again, and I could say that this is going to be an integer. And I could go ahead and say, var, or I could even say, let hash be equal to my constant, hashvalue, and that will be just fine. Now where we're going to run into problems here is if I try to go into my constant And I want to change that to a hundred and I try to save and build that, then that's obviously not going to work because of, once again, one of the definitions of being a constant is that the value, well It's constant, I can't change it. So if you're going to Use a variable or a constant. You just have to keep that concept in mind. If I'm going to assign this value and it's never gonna change, then odds are you're gonna see a lot of documentation and code out on the web that's going to say this is going to stay the same. So, regardless of the scope and how much this is going to be used all over the place, then create a constant. But if you foresee that this might be changing later on or it may be useful to be able to change this variable then I should choose a variable, and as you can see here we'll get a nice little piece of information here that we cannot change a constant. But Swift and Xcode are gonna be nice enough to us and say, well, we can fix that if you want. We could change that let to a var so that we can make it mutable. But, in this case, I don't think that's absolutely necessary cuz we know that we can't do that. And then once again, like I said, we don't have to be explicit about this. We can just call this in. But once again, this won't build because the myConstant has not been set yet. So I can even come down on a second line here and say myConstant is going to be equal to 10. And although that may seem strange, that assignment statement is different than changing the value. So we are able to split this up on both variables and constants where we can declare a variable and its type on one line and then assign it on the next. That's absolutely fine. But I just can't now, in terms of a constant, I can't change it later on. So those are the two basic ways that we're going to create space to store some sort of value, whether it's a in a juror a double or a floater a boolean or a string or any of the other built in types that were going to work with, which we're gonna start to talk about in the next lesson. But this is also a foundational piece of information that you're going to need to understand so that you can go forward and begin to create your applications.

Back to the top