High again in this case we we're going to set a timer for our application.
This is a way for us to visually show the time that has passed since the user started playing the game.
So essentially if I go in here this is the timer that's going to count up the seconds as well as minutes
for us.
Ever since the game begins often we set the reset function or we hit the reset button.
So let's go ahead and see how I'm going to do that.
Feed a few defend valuables for it.
So I go up here but my view this lot.
And I'm going to get myself to your valuables.
One of them is.
I'm going to call it same time.
And that's going to be an integer that is currently at zero.
So that's the current game time that we have to keep adding to it.
The other one is a timer that's going to count the time for us.
So I'm going to call this one game time and that's going to be a timer type and I'm going to make it
an optional value because right at the beginning it doesn't have any values essentially.
So once I have these two value bet I have to go ahead and program my timers to actually do something.
I'm going to do that in the view of the Lord for now but later on you're going to change it and give
it its own function.
So in here I'm going to go ahead and get myself a little bit more space so things are easier to view
and in my view that lot.
And when I go ahead let me get those as well so everything is visible perfect.
So I'm going to go ahead and say first you have to check for something and this check is not important.
Now it will become important later on.
So I have to say if the game timer is currently not to.
If you have already initialized a game timer I know that doesn't happen in the view of the Lord but
it does happen later on and we do things such as when we reset the game because then reset the game.
There already is a timer running.
So we have to essentially get rid of that timer.
If it does exist.
So I'm going to say if it does exist then do this say game tight timer invalidate So don't run it any
more.
And essentially it invalidated.
Then I'm going to go ahead and say game timeout because a timeout d'Arthur schedule fifth time intervals.
And here's the method I want to use.
For those of you who are taking this course as a part of the U.S. completes bootcamp then you know exactly
how it works.
It is totally new for you.
This is the timer that has gone on in this site.
In this case every second is going to go within this very file is going to look for a function that
I haven't made yet.
So we have to create doesn't send any use that information is essentially doesn't use any extra information
and does repeat.
So that's my time.
And now I have to actually to write a function that my time is going to go to.
So for that I'm going to get myself a little bit more space in here and say I have a function I'm going
to call this one timer function.
It doesn't have any problem it doesn't return anything.
It does a few things for me.
One of them is every time we go to a timer function it increases the time.
So a game time increases.
So let's say plus equal 1.
So we get one psych and added that to game time simply because we are calculating based on every one
site.
And if you want to have a timer that goes that every 10 of us I can then hear you to add every tenth
of us I can.
Of course is going to give me an error because Game time is an integer value but you could do it if
you want it.
But for now I'm going to go every cycle and I'm going to add one site and every time we go there then
I'm going to have to shoot it in a label that call the time label.
So I'm going to go in here and say time Liebert dot text.
And now here's something that they have to do I have to format this time to essentially look like this.
2 spaces for the minutes a space a column and other space and two spaces for seconds.
So I have to formatted like this and to do that I'm going to go ahead and say Give me a let let's call
this one time a minute and the timing is going to be a string that I'm going to produce with this format
and the format they want to use is this the string that they want to produce.
Is from anything we tool with spaces and integer from game time.
Divided by 60.
So imagine if the game time is below 60.
So that will stay at zero.
If the game time is between 60 to 100 and 20 that would become of one that's one minute.
For instance 90 seconds it would be 90 divided by 60.
That is one that is integer.
And I'm going to show one extra leading zero for it.
Instead of saying one minute sign it would say zero and one minute sign then that's my minutes and then
I'm going to get the sack.
And so at say time Second is going to be the same everything except that this time you're going to get
time modulars 60.
And that's way I end up getting the remainder of this division.
So real quick example if the number of seconds is 95 game time divided by 60 will produce one.
So we get 1 minute zero or one minute and then this one is going to get 95 modulars 60 and that's going
to be 35.
And that is going to produce 35.
And a second signs and that's all I have to do and I have to put them all in here boy for my formatting.
So I'm going to say the format they want to use is this one good time minutes and then get this space.
They call them get us space and then another format full time science and that's all we had to do to
get this one done.
Now I have to use my timer function in the selector and the moment they do that is going to give me
an error saying it is not an objective c and that's a requirement of the swift version 4.
So now it's an objective c exposed function called the timer function that happens every second ever
since the view loads before I hit the wrong button on this.
I'll have to fix a few things.
I just realized one of them is that I want to shoot two leading zeros so I have to say 0 2 and 0 2.
I forgot to show the sign for the minutes.
So I have to save for the slash minutes and at the end of the seconds I forgot to show the sign for
the seconds.
So that's the seconds.
And one last thing that they want to do is if you do this IF game time is nil is happening and we have
to re invalidate the time it means there was some time being shown and we have to reset that simply
because it will take one second before this thing kicks in.
So in hearing what I say if you have to invalidate the timer then also do this say time labeled dot
text.
I'm here and I'm going to give it a manual a static value.
Another static value is the zero zero for the slash minutes space column the space 0 0 and then forward
the slash slackens and that's going to show us zero zero zero zero at the very beginning.
Every time the game gets reset essentially it's either on the up and we should be able to see a timer
running and showing us the right time 1 to perfect this Schwing us properly.
Of course I don't have the reset function yet and that's something that we're going to add in the next
lesson and on once that we should be able to see the reset actually happening.
So this is all the code for this list and then me to reformat things real quick so we can see everything
hopefully in just one view.
So that is that and that is that and that's where we just get some space in here.
So I added a timeout on all the time we reset the timer and we set a timer and we changed the value
we see on the label every time the timer function runs.
So let's keep it up to here.
Let's go to our next lesson.