Your What's up every one Mark Price you're at slopes dot com and let's go ahead and continue forward
with our app.
But I'd like to do is get the rest of the user interface implemented and get the full application logic
going and then we're going to deal with currency and things like that after the fact because I think
there are some things we're going to have to do to get it to work the way that we want.
So let's go back to the main story board here.
We're just missing a couple of things.
Namely the number of the result.
And as well as the hours label as well as was the clear calculator button.
And again like I said these dollar signs you know they're going to cause us some trouble here and I'll
explain that later on.
So first things first let's go ahead and get our label on here.
Just type in label at the bottom or lab and I'm going to make this a little bit bigger.
So we're going to send you this text and I'm going to make this white.
I'm not going to change any of the fonts around you know how to do that.
And we're just building up the application logic here.
So what I want to do is make this let's just say 32 just like the picture for so I can visualize it
just make this a lot bigger and you know this could be a lot of hours like if you had a car for instance
that was like you know $10000 this might be you know hundreds many hundreds of hours so we want this
to be relatively big.
So what I'm going to do is I'm going to make it like so.
The same with desists at least visually here and I'll control drag to this and we're going to say equal
with.
OK.
Again another thing we could have done was the leading or trailing I said equal with which is fine.
There are multiple ways of working with constraints.
And instead of 46 we'll say 45 from the item price there.
That's looking good.
And the last thing we need to do is probably just center on the screen.
So by click this pin button and do horizontal and container all of our red ink are red lines of anger
and death will go away.
So there's 32.
Let's add a label on to here and we'll go out and just make this a little bit bigger but not doesn't
need to go to the edges because it's always going to say just hours on it make it say hours.
I'm going to center.
Change the color to white and it looks good to me.
Hours and less Pinot seven point five less Pinot.
Five from the top and I'm going to give it a fixed width and height just to get rid of the anger.
The anger lines and then less line at center in the container.
Like so OK.
Cool.
Looks good to me.
And then let's see the clear calculator button at the bottom.
So Type in beauty for button and we'll put this at the bottom.
I'm just eyeballing it here.
I'll make it go to the edges.
Like so the margins there and I'm going to go and click the pin button here and I'm going to say 20
from the left bottom and the right which is great and give it a fixed height.
Perfect.
And instead of button it's going to say clear count you later and Lissa's increase the font size and
let's make it white and there's our button it's all too big I think let's make it smaller.
OK 16 17 16.
OK.
Click save.
So we finish the UI elements let's just do some Iby outlets and stuff.
So we know that we're going to need to hide this label on this label based on the visibility of the
keyboard or the current state of the app so take the 32 label control drag it.
And let's just call this result LPL a result label and then what I want to do is take the hours label
and control drag it over and we're going to call this ours LBO.
All right.
And then I want to make the action from the clear calculator button control drag over here and we're
going to call this clear calculator pressed and let's change it to an action and click connect.
Good good good.
Now close the assistant editor and we'll go into our main Visi swift and get rid of this we got here.
Nonsense.
And then in clear calculator press what do we want to have happen.
Well when the clear button is pressed we want to hide those labels so we want these fields to become
empty again and we want to hide the labels here.
So that should be as simple as doing this.
Well say result label is hidden equals true and then we'll say hours label that is hidden equals true.
And then we will clear out the field so which text text equals empty string and we'll say price text
text equals empty string.
Now since we are hiding them here it would stand to reason that interview to load we would want them
to start out hidden because the apps loading and the keyboards up.
So when you did load occurs we simply have to say result label dot hidden equals true
and result label that is hit and equals SRU and not result label the hours label.
There we go.
OK.
And let's see here.
Looks good.
So when McLure the calculator that's great that's great.
And then when you actually press the calculator button that's where we would that's when we would want
to show those those labels again.
So let's go ahead and write the code to do the math and calculate it and are the red X of years because
our unit tests failed the last time we ran it.
All we need to do is rerun it since our code has been fixed and we'll get to that.
So what do we do if let wage text actually just say wage.
I will call it may point something out here.
I'm calling it Wage text which is exactly the same as this text up here.
But because we're renaming a variable down here we're no longer the things that are inside of it will
no longer refer to the textfield.
OK.
And that's because of the local scope.
So this is creating a new variable even though it's the same name as wage sect's appear it's a completely
different variable.
And some was something I wanted to point out.
OK.
Usually I like to name local variables different when I'm teaching.
So it doesn't confuse you but as a professional developer most of the time you will name it.
The same as other properties so.
So which text equals waged text that text.
And we're going to tape price text equals price text text.
So we're just doing an awful lot here and what we're doing is we're saying hey if the wage textfield
and price textfield is not Neil storeman these variables.
OK.
So calculate will only work if there was actual actually data inside of these fields.
OK.
And now what we need to do is take the text that's in them and we need to convert them to doubles.
Now you're probably thinking well we are keyboard doesn't allow letters and stuff so it's always going
to convert properly to it.
Well it's not the case.
So we can actually copy something on the Internet on safari and then tap and paste in there we can't.
We're not preventing the pasting we're not doing other types of logic there so we do need to do some
validation here.
So again if let wage the same These are going to be double's equals double wage text okay and let price
equals double price text.
Okay.
Are you with me so far.
So the first check was to make sure that the fields weren't empty.
OK and then we store them in these Letz.
If that had failed it would just leave.
And then the second check we do is to take that text and try and convert it into a number k using casting
here so we're casting that number into a double.
And we're doing that here too if these were letters like the letter Z or B These would fail.
OK.
And then it would it would skip out of here and it wouldn't work.
OK.
And you could do air handling if you want else and you can show like a label that says hey your fields
are wrong or things like that.
But at this point here we've got a valid number out of both of these fields and we can actually run
the math.
So first thing we want to do is dismiss the keyboard.
We no longer need it.
So we're going to say View and editing.
So on the main view of this new controller we're going to call end editing and say true to force it.
OK.
No we're going to do is we're going to say result label dot hidden equals false result label is hidden
equals false and ours label dot hidden equals false.
And then the last thing we need to do is just set the data on the results label so result label dot
text.
Equals well doesn't string interpolation.
And we're going to call our function that we wrote earlier so wage get hours for wage.
So we passed in the wage which is simply going to be the wage that we parsed out earlier and price is
going to be the price that we parsed earlier as well too.
And we know that that's returning an integer to us.
And so we're taking that integer that the function returns to us and using string interpellation we're
inserting the integer into the textfield here.
OK.
So if our code is good our app is should technically be close to done at least functionally.
So let's go ahead and run it and see OK.
So let's let's do something some numbers that we know what works so hourly wage if our hourly wage is
twenty five dollars an hour.
And the item price is one hundred.
If I click calculate this should say four hours.
That's pretty good.
All right.
So if I clear a calculator these should all clear out good.
And what I want to do is a crazy number.
So two twenty five point twenty five divided by let's say fifteen point fifty.
OK so two twenty five point twenty five and 15 50 should give us 14.
So we should see 15 here so when I put these numbers in the value that we should see on the screen should
be 15 hours to get this item so to 25 25 k 2 to 5 at 25 and the hoops this one is 225 to 25 at 25 and
this one is going to be 50/50 we go.
OK.
So if I click calculate 15 hours.
OK.
And it rounded up exactly what we needed.
So 15 hour so this is perfect.
This is working.
Here's the problem though our dang designer.
Ok it's always the frickin designer man is making our life miserable.
And I get the dang designer called for dollar signs in front of here and this is actually a big problem.
It's really funny when it comes to programming things that you wouldn't think are big problems are big
problems and then it's vice versa things you think things that one might think is HUGE might actually
be really simple.
So here's the problem.
This dollar sign here there's no there's nothing we can do about it.
Like like it doesn't come pre-built in a field this is a currency field.
So what we have to do in order to get this to work is we have to write some textfield delegates and
every time the user types we detect it and then we basically we we strip out all the characters out
of it that are not dollar signs and then we use those and we convert those in the numbers and things
like that and we make sure that the dollar signs are.
And we also have to write code so when the back button is pressed that it won't let a delete happen
whenever it's the very first character which is a dollar sign.
Very very complex quote I've actually written this code before and it's a nightmare.
And there are some free libraries that do this for you but it's a big nightmare.
So what I would do is I'm going to let my designer know hey man this is this is really rough.
Ok this is this is hard to do.
And it's going to take a lot of time for a very little function it could take me a few hours or even
all day if you're new developer it could take a couple of days.
You know you'd have to figure it out.
It's not very easy.
So I'm going to propose instead what we do is on the left hand side here we actually put an icon that
shows the currency.
So if it's in United States of the US dollar it's a different currency based on whatever country you're
in and it'll show in a little icon here and then we don't ever have to manipulate the actual text itself.
And so my designer he shakes his head he's like yeah I think that's a great idea Marc and I'm like yeah
I think it's a great idea to say I mean lots of time and boom.
Now I'm a designer and a programmer so let's go ahead and do that.
I've just convinced the designer of what we need to do.
And so we're going to go ahead and make a really pretty icon here but we're going to do that in the
next video so we'll wrap up the entire project with the cool currency formatter.
And you have a full fledged app ready to go soon.
So Mark Price it does soap's dot com.
Let's get this done.