Hey what's going on everybody this Caleb with Dev slopes dot com and we're going to finish up our binary
digit converter app.
In this video.
Believe it or not what we're going to do is we're going to set it up so that we can enter in a value
we can tap on a button either binary or decimal and it will do the appropriate conversion which is very
cool stuff.
It's really simple actually we've already written all of the complex code to make it happen.
And let's do it.
I don't know about you.
I want to get started.
So let's do it.
We're going to basically be in our view controller file here and these are our two Iby actions.
So essentially what we're going to do is we're going to make sure that we have a value in our textfield
and if we do if we push the binary button we're going to go ahead and make the decimal button dim.
OK so it looks like we select it and then we're going to make the decimal button eliminate and the binary
button dim when we select the decimal button so they can go back and forth.
Pretty cool stuff.
So let's actually go ahead and write that.
Now let me give us a little bit more rooms we can see.
There we go.
And basically let's just check to make sure there's a value if value entry textfield that text is equal
to nothing.
OK or sorry it's not equal to nothing.
Meaning we have a value.
We're going to go ahead and set the binary button to be 100 percent as far as it's alpha channel goes.
And then we'll set the decimal button to be 50 percent just like a peer.
So go ahead and type a binary button.
Alpha equals 1.0 and decimal button that Alpha equals zero point five.
OK.
So that will make the binary button illuminated and the other one faded.
Next what we want to do is we basically want to take the value from the textfield whatever text is in
there and convert it into an integer.
And we know that we're OK to do this because we're using a number pad.
The user can only input numbers anyway so go ahead and type guard let string equals value entry textfield
text.
OK.
And then type a comma and we can create a second constant here called in from string and we're going
to basically cast our string as an integer.
So Type let it from string equals int and then we're going to pass in the string value to that.
So now it's an integer.
So if we typed 1 0 0 1 1 0 1 1.
Now it's actually that integer value since we're using guard let go ahead and type.
Else return.
And now we have a constant for both.
So if there is text we can create it using guard let.
And the reason I'm using guard let is because even though we know there's a value it's still an optional
property.
So we're using a guard led to basically create a constant if there is a value and otherwise we'll just
return if there's no value so that's good because that will avoid a crash.
Then we use it from string to force cast the string here as an int and that's cool because normally
this is an optional and you have to force unwrap it.
But this is a safe way to do it.
So if we can't if for some reason they typed in letters if they could somehow figure out a way like
maybe they accidentally pasted some letters in this would just return and it wouldn't cause a crash.
So below that we're going to go ahead and use our binary decimal class to create an instance and pass
in our integer as a decimal.
So go ahead and type let binary decimal.
Well let's say let.
Maybe binary digit.
And we're going to use our class here binary decimal and we can call our initializers here we have two
of them.
One is decimal and one is bits.
This one is going to be a decimal.
So select decimal.
OK delete it and we can pass in in from string.
Now we can go ahead and actually set the value of our textfield to represent our new converted number
so we can say value entery textfield dot text is equal to a string.
And we want to actually use our binary digits so we can use something called String encapsulation K
and we can pass in binary digit.
But that's that's the instance of our class in order to actually get the binary value for the integer
that we just set.
We can call DOT calculate binary value for int and that'll print out the binary value.
You want to try it out.
Let's do it.
BuildOn run.
Let's go see how it works.
And then we're going to go ahead and move on to this decimal Iby action.
OK so we enter in a value let's try well 145 that's been the number that we've been using.
So we have a value when we tap the button the decimal button should go down to 0.5.
We should basically calculate the binary value for this decimal and then replace the value entry textfield
text with the binary representation.
So click binary.
Look at that.
That works that's exactly right.
1 0 0 1 0 0 0 or 1.
That's exactly right.
That's what we need.
Now we're going to write the Iby action for the decimal button so that it can convert it back into a
decimal.
OK.
So let's do it.
Very very cool stuff though.
So we're going to use the same validation to make sure we have a value and so to do that we're going
to type if value entry textfield dot text is not equal to nothing.
Meaning if there's something there we're going to go ahead and do the opposite of above.
We're going to set binary button 2.5 and decimal button to 1.0.
So binary buttons Alpha is equal to zero point five and decimal button Alpha is one point zero.
Then we're going to do something similar to what we did before.
We're going to use the Guard letter to create a string for the value entry textfield.
But basically what we're going to need to do is we're going to need to take each individual item from
our string K from the string from the value into textfield and convert it into an array of integers
k because remember the other way we can create a binary decimal is from an array of integers of ones
and zeros.
So let's do that now.
So type guard let.
String equals value entery textfield dot text.
Else we're going to go ahead and just return.
OK.
Now we're going to use that string to essentially turn it into an array of integers and we can use the
map function for that's very helpful.
So go ahead and type let bits from string equals string dot map.
And as you can see we can actually transform each character just like we did before.
And we can actually get rid of these parentheses here you don't need them.
But in the curly brackets inside the closure what we can do is we can take the dollar sign zero value
meaning every character of the string which if we type in binary would be you know 1 0 1 1 0.
It would be a 1 or 0.
So what we're going to do is we're going to make sure that that's a string K like so OK just to make
sure that it's a string coming in and what we're going to do is we're going to encapsulate that as an
end.
OK we're going to force cast it to be a forecastable.
We're casting it as an integer.
Now if I try to build this it's going to say hey error can't be actually done.
We have to actually force on Rapide here to turn it into an integer k because remember a string could
be an integer or it could not.
And that's why it's optional.
So we're going to unwrap it there and make sure that it is in fact an optional urn or make sure that
it is in fact a number so we can actually cast it.
Now the interesting thing is is that map is going to return an array of whatever value we go through
so we're cycling through every character in the string.
What we're doing is we're converting them into an integer.
So now we have an array of integers ones and zeros and then what we're going to do is we're going to
create an instance of binary decimal.
So go ahead and type let binary digit equals binary decimal and check it out.
I can call the constructor here and we can pass in our array of bits which is bits from string and that
will allow us to create what we need now that we have now that we've done this we can call our function
that we just wrote calculate value for binary.
So let's go ahead and set the value entery textfield text to be equal to a string.
Just like above.
Use string encapsulation to pass in the binary digit K from this function and we can call calculate
value for binary.
So let's go ahead let's build and run this let's see if what we did worked to convert our decimal numbers
into binary.
It should change the colors and the opacities of the buttons and it should successfully convert the
value.
So let's actually begin with a binary value.
Let's go ahead and begin with.
1 0 0 1 0 0 0 0 1.
If you remember that should be 145.
And we're going to convert this into a decimal when I push this.
It should convert to 145 3 to 1 click and it works that's amazing.
So since there's a decimal here now I should be able to click binary and switch between them automatically
and look at that.
It works.
That's exactly right.
Really really cool stuff.
So we can now successfully convert decimal numbers all the way up to binary numbers.
Now there are of course are many many ways that this app could be expanded and made better.
You could make a you know a secret thought translator that you could type in words and convert them
into binary.
We'll get to that in the challenge video guys.
Amazing work.
Our app can successfully convert between binary and decimal numbers.
Super super great work.
Let's head over to the next video where you're going to have a quick challenge to make this app even
cooler.
I'll see you there.