So let's discover what variables are using an Xcode playground to open up Xcode, go to file new playground.
Now playgrounds just let you mess around with basic code and give you the results instantly so you don't
have to create a brand new iOS project or anything crazy like that.
Hit next on blank and save it wherever you want to save it.
So mine's going straight to the desktop.
Now, let me resize this for you.
And you'll see that Xcode has pretty populated some stuff in here.
First of all, it's imported UI kit, which allows us to run certain things in the code.
Don't worry about that for now.
Imports will be part of the course as we go along.
What I want to focus on is this thing vare SDR, VA stands for variable and all the variable is at its
basic level is something that's stored in memory.
In fact, everything in your program is technically a variable if you use that definition.
So functions are stored in memory classes and objects are stored in memory.
So they're technically variables, at least in my opinion.
The next section is the name of the variable, so in this case, star, now SDR is an awful name for
a variable because if I come back to this code in six months, I don't know what Star stands for.
Do I?
Neither do you.
If I come to your code and I see that I'm liable to not want to help you with your code because nothing
is obvious.
So if I was going to give this a better name, I would call it something like welcome message.
And then everyone knows what it is.
But I'm getting ahead of myself there.
I do, however, like to have students trained in naming things correctly so their software and code
is readable anyway, back to the subject at hand.
So we have a variable string or star, which is this thing surrounded by quotes and that is a string.
A string is simply a collection of characters in order.
So that is simple enough.
Now where do we go from here?
Well, what we can do, you see this little blue icon at the side, if you hover over it, if you hit
play on that, you will see over on the right hand side.
The result of what you're doing there now, this is currently running and now it displays hello playground.
These things are much faster to run the second time because it's already compiled a lot of stuff in
the background.
So that just spits out our variable.
Now, what if I want to spit out this variable in the console and I'll show you what that means in a
second.
But we're going to run something called a print.
And if I press play on that, look what pops up at the bottom of the screen, we have Hello Playground.
So that print statement prints things out for us, and you can use that to debug your programs as you
get more advanced, so you'll print out this method isn't working or this error came back from the server
or whatever it is, and the user doesn't see it, but use the developer will be able to see it.
Right.
Let's create another string.
So var my let's create another string with an awful name.
My string is equal to what let's call this ground.
So actually a better name would be name, but we'll leave it as it is.
Now how does Swift and iOS and Xcode and all the rest of it know that this is a string while we've put
it between quotes.
So that's how it knows.
But we can tell it explicitly.
By putting a colon and then the word string with a capital S. So that tells it this is a string and
if I start typing that again, you look at that little handy hint that pops up string a Unicode string
value, that is a collection of characters, like I said.
OK, so you can explicitly tell it so there are two ways of declaring variables, really.
One is implicit let the system guess what it is and the other one is explicit.
Tell the system what it is you'll get used to.
Which one to use as in when?
Because it varies.
Right.
And I know lots of programmers who just let the system guess the whole time.
I would say that decent programmers as well.
OK, now, of course, we can do the same here, we can print my string.
And if we run this whole lot, we should get what we need now, notice when you print my string, we
get this backslash in at the end of my name there.
Why is that?
Well, that's the short code for a new line, what you call it, carriage return a break.
So it goes to another line.
That doesn't actually get printed out, as you can see down here in the console, you just get the word,
but the new line is printed because the next print statement happens on a new line.
Right, so that's how we implicitly and explicitly declare a variable.
Now, why is it called a variable, do you think?
It's not constant, it's not called the constant, there are constants, but not in this case variable,
because it is variable.
So I can change my name.
To.
Mike, because we can wipe whatever's in that memory space and put something new in it and allocate
more memory if need be, I'm not sure we can allocate less memory.
So I guess that's something to watch out for, right.
So variables are precisely that.
They are variable.
Now, what are we going to do next?
Well, let's look at the final category in here, which is the constant.
Now, in lots of languages, we use the word const or perhaps even constant to define something that
cannot change.
So if I wanted my name to be set in stone, I would set it as constant, unchangeable.
The way we do that and Swift is let.
My name is Quilty.
Can't even spell my own name.
Grant, right?
So that's perfectly fine, that will run, but if I run, my name is equal to Mike.
Watch what happens.
Just wait for the system to catch up.
Cannot assigned to value.
My name is a let constant.
Right, because it is constant.
Something of note for everyone, beginning programming their programming journey is wherever you can
use a constant right.
It's really good practice to keep things constant and to not modify variables on the fly unless you
have to, because you do have to on occasion, in fact, maybe half the time.
But constants mean that you don't have what we call spaghetti code crossing over each other.
Everything's changing everything else.
And then when you get a bug, it's really hard to track down.
So constants are your friend, right?
I'm going to set you guys a little bit of homework here.
Now, what is it?
I've got my notes here.
Create a string variable with your name in it and print that string to the command line.
So I want you to create a string that assigns your name to it.
It can either be variable or constant.
And then I want you to print that the command line.
Off you go.