Hi Alex here from rebeloper.com and welcome back to another awesome tutorial
in the extensions series. Now today we are going to tackle time interval.
In the previous one we have talked about strings, so go ahead and take a look at
the previous video to understand how the starter project works. So without further
ado let's jump right into it!
Okay! Let me just show you what we are going to build
out today. I am just going to copy time interval from here and go put it in the
there and build and run the test project. Let's see what we are going to build
out today with the help of an awesome extension. It says time in your timeinterval.
Okay! Let's say... and of course this is in seconds. Let's say 100 and as
you can see it typed out as a string. One minute and 40 seconds and of course that
is indeed 100 seconds. Let's take a look at some random number here and so on
maybe not that much 10 hours 49 minutes and 5 seconds. So that is what we are
going to build out today. Let's get started! Okay, let's stop this test
project and dive deep into the starter project. Again go ahead and take a look
at the first video in this tutorial series. Now let me just go ahead and
again choose time interval for the example type and let's take a look at
the extensions again. As you can see here we have already set up as time string
function and currently it returns an empty string. But before we dive deeper
into creating this extension, let me just show you what the ViewController looks like.
Here we go! Just find the TimeIntervalViewController.
Here we have a text field and a label that will output the string that
we have created from our timeinterval. And we are setting it on the view and
that is basically it. We have a handle tax change what we are going to use
later on... well basically this handles the text change in our textField.
Let's go into the extensions and create our TimeInterval
string. What we want to achieve here is add hours minutes and seconds.
Let's add in here: let hours = 0, so we get an int and
then: let minutes = 0 again and let seconds = 0 again. So what we
want to return here is a string with a format. Go ahead and choose this
one. And the format will be like follow: (format: "$02i: ",).
And we just simply copy and paste that one two more times and delete.
There we go! And the arguments will be: hours, minutes, second. Okay! Now, yeah
this is going to work now. But for now we are going to export all of these zeros
like in two strings. Let me just go back to our TimeIntervalViewController
and just simply go ahead and have our labels text
to be... let's say I'm just going to add in here an arbitrary text or maybe let's
just select the textField text. But for that we need to unwrap it so let's go
ahead and do that now. So: guard let text = textFiels.test
and as you can see this is an optional. So we needed
to unwrap this... so if that doesn't exist we return. Now this text needs to be
converted into a Timeinterval. Let's do that like so: TimeInterval we add in
here a text and a .asTimeString. Now this is again a time an optional so
let's guard let unwrap that and I will talk in here duration or
something like that = TimeInterval(text) else we return.
Okay! And now I can just simply grab this duration and set it in here. And that is
all we need to do in the handle text change. Okay! For now we are going to
have all the time zeros so let's build and run and see how this works out.
Let's just type in here some values. And as you can see the values here don't match
just double check all of the formatting in the tutorial on my test and let me
just simply copy and paste this is here. Don't peek! I will just add it in
here. There we go! Yes! So what do we need to have here?
Well first of all there are 3060 seconds in an hour so what we need to have here is
add in here Int(self) because we are adding in the TimeInterval.
And let's divide this by 3600. Again here we go Int (self). And again
here Int (self). So how many minutes are there? We need to divide this by
60 and grab the modulus of that. And the second is the modulus of 60 so the
remainder of 60 modulus. Okay and that should work out pretty straightforward.
Let's see if this really happens once we build and run. Okay! There we go!
Let's type in here 12 and indeed once we did add the self into the equation we do get
back the correct form. So 12 seconds let's add here 200 and there we go 3
minutes and 20 seconds! Now that is it for this
tutorial on TimeInterval extension! And we extended the TimeInterval to be
represented as a time string. I hope you enjoyed this video if you did go ahead
and give it a thumbs up subscribe to the channel and hit that notification bell to
get notified of the next video. And in the next episode we are going to take a
look at dates. So we are going to take a look at a Date extension. Really
interesting stuff! So I will see you in the next one!