Hey buddy what's going on this is Caleb with Deb's slopes dot com.
And in this video we're going to be talking about variables.
Now you're probably thinking wait variable's that's a really basic easy component of Swift I already
know variables.
Not like this you don't.
In Arek Swift a variable is actually a custom version of a behavior subject which is a custom version
of an observable so a variable in our X Swift can actually be used just like an ordinary variable.
But of course you have the same subject functionality that you do with behavior subjects and published
subjects but it works in a little bit different of a way.
So that's why I'm going to show you.
So go ahead and pull open your Rick's playground and we're going to go ahead and copy those three top
lines that we've been using over and over again.
And we're going to right click on behavior subject and create a new playground page.
We're going to name it variable and then we're going to go ahead and select one of the numbered lines
here.
Select all and delete.
Then go ahead and paste in your import our swift import playground support as well as the needs indefinite
execution line.
So that's cool but if you build it if you clean it let's clean it there.
You might notice that it will say that it cannot access our X Swift.
Let me go ahead and give it a shot here.
Let me see if we actually do have access.
OK.
So observable is not showing up.
That gives me a clue that we do not have it properly imported.
I don't know if you see that that error message at the bottom is saying no such module.
So let's fix that like we have before.
I'm going to save this.
Close it and reopen it.
And as soon as it opens up we can go ahead and build it.
And now we should have access to our observables.
There we go.
Cool cool.
So that's just a really easy way to clean out the project and get it set up with swift.
So we're going to create a variable which is a type of observable.
Now go ahead and do that by typing var variable.
Kind of redundant equals variable even more redundant.
And we need to give it an explicit type.
So using the brackets go ahead and pass string in so that it knows that it's of type string.
Now a variable is just like a behavior subject in that it needs an initial value to begin.
So think about it it's just like an ordinary variable if you're going to use a variable that has to
have a value.
So go ahead and pass in a string here saying hello.
And now we have a variable observable that has a value of Hello.
OK.
Easy enough.
Now what we're going to do to use it is different than behavior subjects and publish subjects a variable
in and of itself does not register as an observable because it can be used in a couple of different
ways.
If we want to use our variable as an observable we need to tell our ex-wifes swift so.
So go ahead and type variable dot as observable.
Do you see that.
And you know if we look at the description of this it says it's a canonical interface for push style
sequence.
That's not very clear but basically what you need to know is that this is going to convert our variable
into a variable that is observable.
OK.
Otherwise it's just sort of like an ordinary variable you know what I actually I don't know if it's
possible to do this let's try to just print the variable and see what happens if we printed out OK.
So it shows up as a type or swift variable Swift got string.
But I wonder if we were to pull out the value of that if it would print out.
Hello.
Okay.
And it does so.
Just so you know that's new to me as well.
You can go into a variable and pull out its value.
That's pretty cool.
So anyway we have now cast our variable here as an observable.
And next we're going to go ahead and try to subscribe to pull out the value.
So go ahead and type.
Dot subscribe and inside of Subscribe we're using on next just like we have in our previous videos with
other subjects and I added some curly brackets there so that we have a closure that we're using the
next closure.
And basically we have already passed in a value we've already called on next once and so we can go ahead
and we can print whatever that value is by using the dollar sign 0 K that's a temporary variable that's
going to cycle through this whole sequence called variable and print out whatever is in there.
And as you can see Hello Prince right out to the con..
Pretty cool.
So since this is a variable we can't pass in New values right.
A variable can only have one value at a time.
So if I try to go in and type variable dot on next doesn't work if I try variable as observable on next
it doesn't work.
OK.
We cannot use this in the same way that we have been using other subjects and other observables.
Think of a variable just like an ordinary variable.
It's sort of like you're probably going to be most comfortable with variables.
This type of variable with the capital V from our X Swift is going to feel a lot more natural to use
if you're a non-reactive programmer which if you're taking this course I'm assuming you are we can use
it just like an ordinary variable.
All we need to do is call variable value and we can pull out the value that we want Ok that is variables.
I mean honestly I wish that there was more I could tell you about but this is it.
Variables are really simple and are swift just like they are in ordinary swift So this is Caleb with
those slopes dot com and in the next video you're going to have a challenge.
I'm going to challenge you to use your knowledge of subjects to build something really cool in code.
So head over to the next video for your challenge.