Everybody else is gone this is Caleb with Dev slopes and we're going to be revisiting a project from
earlier in this section and it's the one where we built this calculator app OK we made an actual project
here.
We have a view controller of course we have some incomes for various things.
And then in our model section we have what's called a calculator manager or I guess I should say a calculation
manager.
Now the interesting thing is is that this works very similarly to what is called a view model.
And a view model is essentially a layer.
Well you're familiar with model view controller and you know what each of those are individually Model
View view model is another paradigm where there is of course models.
There are controllers and then there are what are called view models and a view model is basically responsible
for converting the data objects from the model and then presenting them in a easily displayable way.
And that's basically what we're doing here is we've basically created a view model accidentally.
This handles all of the logic that otherwise would need to be written in the view controller but we're
abstracting it out of view controller and into a much more manageable layer called a view model so we're
actually going to go ahead and rename this calculator calculator or view model.
All right.
And of course if we're going to be renaming this we need to rename everything so look for a calculation
manager and we're going to replace that with calculator view model.
OK.
And click.
All and it's going to go ahead and replace all of those and we should be good in here there isn't another
instance of calculator if you model.
But if I try to build this we're going to get a lot of problems particularly in the calculator View
Controller and in our tests.
So we're going to go ahead and do the same.
Find and Replace find calculation manager and replace with calculator view model and actually the process
that we use to do this.
Creating an instance wups shoot well we're going to go back and put lowercase C's here I'm going to
copy this anywhere that we are using that instance we're going to replace it.
Of course copying and pasting is never the best option but it is an option.
So we have a calculator view model here.
Let's actually rename this file to Kelk Q. later or view model Swift.
This is also calculator view modeled out Swift.
And there we go.
Very cool.
Now if we look around make sure that we've done everything the right way.
And if we come in here we can go ahead and type calculator view model tests.
OK.
I'm going to rename the file that as well.
And I'm going to name this commented file name the same thing.
Now of course calculation manager no longer exists but calculator view model does exist.
And so this is going to be a set of tests to actually test our calculator view model to make sure that
it works the same way as before and you know what I don't actually see any other instances here so I'm
going to build it and see if it all builds successfully which it does.
And so a view model allows us to essentially separate all of our logic like we have here.
All of all of the things that basically convert the values perform the calculations.
Basically what works with the data and creates it into a presentable way we can access through our instance
of the view model so we can basically just create an instance of it and then call all of our logic on
our view model which is very cool.
So let's go ahead and let's build and run our tests.
Let's make sure that they still run properly and work to make sure every single test passes and OK one
test failed.
So what is up with that.
OK so it's saying zero is not equal to 25.
We must have changed something here.
Let's take a look.
So when we push a number button it appends it to the array.
Oh but it never converts them back into a string and sets the labels so let's go ahead and type value
label dot text and that should be equal to our view model dot string of five values because if you remember
that basically takes our array which when we append we add 1 to the array then it converted into a string
so it can be displayed in the label.
That's super weird OK.
So let's try running those tests again.
This is why testing is super helpful because it helps you to catch these weird problems that sometimes
you might accidentally introduce if you were to accidentally delete a line of code and check it out.
It says all test succeeded.
Let's go to the test navigator here and everything works.
Our view model is successfully instantiated and this is how you use model view view model to test view
controllers.
It makes it much easier to actually test the logic which you want to test.
So anyway this is Caleb with Devis Loeb's dot com let's head on over to our exercise.