Solo come back again in this lesson we're going to do something quite simple and quite often useful.
And that is how to start a timer.
So I'm going to go to school with this thought a action when I start the new playground this time you
haven't done that in a while.
So when I say play along.
This one is going to be time.
They go on.
They're
out.
So what they want to do is this it's a very simple thing.
And then I say class I'm going to call it my class and then my class is going to have essentially two
functions one that runs a timer and the other one that happens every time the timer runs itself.
So a function through the trigger timer a function that the timer calls.
So essentially one of them is like saying hey run the timer.
The other one is saying so in every Sikander every two seconds or every 10 seconds what do you want
to happen.
So that's a simple.
When I say function and my function I'm going to call it a run timer has no promulgators no return type
in the body I have only one command timer DOD schedule time of it's time intervals and user info.
So there's a lot of information here.
Let me quickly explain what you have to do with them.
So the time interval is how often you want something to happen.
I'm going to say every socket and this a floating number.
So you could for instance say every every millisecond every Thousands of a second.
What fallen out one target is going to be self there's going to be a function right in here.
The selector is going to be a method that we need to add.
So that's the other function I'm going to say.
The other one is called the let's call it preens fine grained time.
So all we're going to do is going to print time now but you can simply imagine how you would expand
on this to make a clock to make even make an hour long closed by using transformations.
So on and so forth.
But for now we're just going to say print on that print is going to say right for instance.
Now my selector is going to be this method.
Now here is a change that you have to essentially be aware of as of X Strief version 4.
You need to expose anything that is using a select or two or Objective C.
That means you have to have an Objective C in it.
And I will show you in a second what happens if you don't have it.
Let me say there is no use anything for let's say it is going to be repeating itself.
Let me opt out and we'll see the error.
So now it says argument of selector is not exposed to Objective-C and this is a necessity as of swift
version 4.
So if you want we can actually go to a terminal and say X code wrong Strief the dash version and it
blue I am using version 4 point two that is essentially a command from the export command line tools.
If you don't have the execute command line tools which offers a lot of flexibility when you're working
with projects and we'll see it sometime in the future as well.
But for now if you don't have anything in between a story you can of course do a quick search for it
to come command you will end up meeting is X code select dash dash and saw and that's kind of style
X code command line tools.
So ex-coach space dash select and then install.
So that's not what we're doing.
We want to know the version of the swift that we using they're using 4.1 or anything of all four.
You need to expose your functions in selectors to an Objective C by calling Objective C.
I'm guessing sometime in the future are they going to they're going to change that.
But for now this is how it works.
So let's go ahead and use this time.
So to use that I'm going to go ahead and say I have a variable called my object which is of course my
class and then my object draws the wrong time.
So it runs that time.
Let's go ahead and see what that does for me.
And it says hi.
Hi.
It constantly says hi once every once again.
Now let's go ahead and make this a little bit more exciting than Mythili dad so I can get myself a little
bit of the spaces in here.
The Dow was though.
All right.
So here I'm going to do.
I'm going to set you my class.
I have something called our odd time and that's going to actually be a float.
It actually has a value of zero at the very beginning.
And then here is what's going to happen every time we go to print time.
We're going to same time plus a call let's add a 10 office I can't.
And then let's go there every tenth office Hikind.
Now when I say preens time is on time is going to be time.
Now if we run this you will notice that the float interpellation doesn't return us the exact numbers
we were expecting.
So we kind of meet the way of rounding it or showing only one similar space and to do that I'm actually
going to change the print argument in here.
I'm going to say the argument is I want is a strong format and the format I want is going to be percentage
dot to one decimal space and the value is time itself.
So essentially I'm saying format is fine.
You at one decimal space and print it for me.
Let's run this again.
And now we get exactly what we expected it to.
Three point forty four point one.
So on and so forth if you wanted to I could go ahead and say call every hundredths of a second and give
me tuus faces for instance is going to run a lot faster.
In a lot of cases you want to go every one second or anything of the source.
Of course you know how to connect all of these things into variables so you don't need to change it
entirely.
Places can change it only in one place and that's about that.
Running a very simple timer.
So I'll see in the next lesson.