Hi, Alex here from rebeloper.com and welcome back to this tutorial series on
some awesome extensions and today we are going to take a look at our first a
double extension. So without further ado let's jump right into the code.
Today we are going to take a look at our first double extension because there will be
two double extensions. The first one will be a converting extension and we are
going to convert kilometers into miles and we can do other way and with some
other types too like centimeters, meters and so on. Now let's take a look at what
we are going to build out. Let's change the example type to double. If you are
not familiar go ahead and take a look at the first video in this episodes series
about this starter project and where you can download it basically it's clicking
down the link below and you will find it. So let's take a look at the testing
extensions fun project that I have already built out and as you can see it
says type in your distance in kilometers. So let's say it's simply 1 and that is
real 0.621 miles and I believe we should add in
here miles so hopefully in the project that we are going to build out today I
will add that in there. So let's jump right into it and build out this
extension. So let's close that and currently we have our extensions here
convert from original unit to converted unit and a double and it returns 0
basically. Now we are going to add that in here. But before we do that let's take
a look the app delegate because we want to
change our example type to double as we did in the
test project and we might as well take a look at the double view controller.
We have a text field and a label where all of this will be printed out, okay. And we
have a handle text change added as a target to our text field. And that is it.
We are going to call the extension from here and we are going to create the
extension in this function convert from original unit to converted unit. So let's
just do that. Let's create a measurement and let's choose this value. The value of
course is the double so we are going to add self here and the unit is of course
our original unit. Now what you want to do is add a dot here and convert to and
again we have another unit length. So let's type in here to the converted unit
and we want to have our value grabbed there and I believe that is it.
Let's just hit command B to see if everything worked out and that should be it.
So how are we going to use this in our application.
Let's go back to our where it is? It is our double, double view controller
and inside here we want to grab the text from the text field so let's guard let
unwrap it our text from our text fields dot text, yes we want to return, okay.
And once we do have that text we want to set it as a double. So let's add in here let
distance equals double the text and now we want to add this
distance as the converted value to our label. So label dot text equals distance
and this is an optional double so we are going to add in here as and I believe we
should have convert, yeah there we go. Convert and the unit length will be
kilometres and to let's say miles. There we go. Now what are suggest is guard let
unwrapping the distance to be sure that we do get a double here and again as I
mentioned in the beginning of this video so let's delete this we need to add a
miles here. So let's just add in these to be a string and now let's unwrap this.
There we go and let's add here miles, okay. And that is it
rather yet not there just need to add this here miles or if you wanted to be
even more illiquid then we would just simply create and let's just do that.
I believe these are let just take a look at the types of these they are unit length.
So let's just go all the way up here and create two unit length. So let original
unit length and it will be of type unit
length and it will be kilometers, okay. And a let and how did I call it and
let's just go into the extension and converted unit length. Let's just copy
this out for convenience and paste that in there. Again it is of type unit length and
it will be miles, okay. Now we can use these two in our project. So here we have
our original unit length. So we add that into our text fields text and let you
scroll all the way down and add our from to be original unit length and here we
have our converted unit length and of course let's copy this out and have the
miles add it in there dynamically and that should do it let's just build and
run and see if this actually worked and that I would just switch the tubes so
maybe, maybe we need some more clearance here. Let's just scroll all the way up
and let's say description that should work and description here too.
Okay! Let's just build and run and see hopefully the correct text there.
And it is not correct rather yet let's change this back to miles and up here
kilometers, okay. Let's build and run and then we should see the change, okay.
There we go. And one and that is 0.613 and so on
miles and this long number where the decimals are long that is because it is
a double. Now in the next video we are going to tackle this long number and
we are going to have 2, 3 or whatever lengths do we want to have this
number of characters to be. 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 new videos coming up and as usual I will see you in the next one.