1
00:00:05,960 --> 00:00:11,420
Hey what's going on everybody this Caleb with Dev slopes dot com and we're going to finish up our binary

2
00:00:11,480 --> 00:00:13,090
digit converter app.

3
00:00:13,370 --> 00:00:14,240
In this video.

4
00:00:14,240 --> 00:00:19,010
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

5
00:00:19,250 --> 00:00:25,070
we can tap on a button either binary or decimal and it will do the appropriate conversion which is very

6
00:00:25,250 --> 00:00:26,990
cool stuff.

7
00:00:27,020 --> 00:00:31,360
It's really simple actually we've already written all of the complex code to make it happen.

8
00:00:31,650 --> 00:00:33,440
And let's do it.

9
00:00:33,440 --> 00:00:34,260
I don't know about you.

10
00:00:34,280 --> 00:00:35,160
I want to get started.

11
00:00:35,210 --> 00:00:36,590
So let's do it.

12
00:00:36,620 --> 00:00:42,440
We're going to basically be in our view controller file here and these are our two Iby actions.

13
00:00:42,440 --> 00:00:47,700
So essentially what we're going to do is we're going to make sure that we have a value in our textfield

14
00:00:48,080 --> 00:00:53,610
and if we do if we push the binary button we're going to go ahead and make the decimal button dim.

15
00:00:53,700 --> 00:00:58,700
OK so it looks like we select it and then we're going to make the decimal button eliminate and the binary

16
00:00:58,700 --> 00:01:03,110
button dim when we select the decimal button so they can go back and forth.

17
00:01:03,110 --> 00:01:04,460
Pretty cool stuff.

18
00:01:04,460 --> 00:01:06,150
So let's actually go ahead and write that.

19
00:01:06,140 --> 00:01:09,530
Now let me give us a little bit more rooms we can see.

20
00:01:09,530 --> 00:01:10,480
There we go.

21
00:01:10,730 --> 00:01:17,060
And basically let's just check to make sure there's a value if value entry textfield that text is equal

22
00:01:17,060 --> 00:01:18,790
to nothing.

23
00:01:19,160 --> 00:01:22,710
OK or sorry it's not equal to nothing.

24
00:01:22,820 --> 00:01:24,650
Meaning we have a value.

25
00:01:24,650 --> 00:01:30,640
We're going to go ahead and set the binary button to be 100 percent as far as it's alpha channel goes.

26
00:01:30,860 --> 00:01:35,600
And then we'll set the decimal button to be 50 percent just like a peer.

27
00:01:35,610 --> 00:01:38,660
So go ahead and type a binary button.

28
00:01:38,720 --> 00:01:46,380
Alpha equals 1.0 and decimal button that Alpha equals zero point five.

29
00:01:46,680 --> 00:01:46,960
OK.

30
00:01:46,970 --> 00:01:51,390
So that will make the binary button illuminated and the other one faded.

31
00:01:51,380 --> 00:01:57,920
Next what we want to do is we basically want to take the value from the textfield whatever text is in

32
00:01:57,920 --> 00:02:00,220
there and convert it into an integer.

33
00:02:00,230 --> 00:02:03,690
And we know that we're OK to do this because we're using a number pad.

34
00:02:03,770 --> 00:02:11,470
The user can only input numbers anyway so go ahead and type guard let string equals value entry textfield

35
00:02:12,000 --> 00:02:12,790
text.

36
00:02:12,960 --> 00:02:13,720
OK.

37
00:02:13,880 --> 00:02:18,590
And then type a comma and we can create a second constant here called in from string and we're going

38
00:02:18,590 --> 00:02:22,170
to basically cast our string as an integer.

39
00:02:22,210 --> 00:02:30,530
So Type let it from string equals int and then we're going to pass in the string value to that.

40
00:02:30,530 --> 00:02:31,710
So now it's an integer.

41
00:02:31,730 --> 00:02:34,790
So if we typed 1 0 0 1 1 0 1 1.

42
00:02:34,880 --> 00:02:39,860
Now it's actually that integer value since we're using guard let go ahead and type.

43
00:02:39,860 --> 00:02:41,960
Else return.

44
00:02:42,290 --> 00:02:44,390
And now we have a constant for both.

45
00:02:44,390 --> 00:02:47,740
So if there is text we can create it using guard let.

46
00:02:47,750 --> 00:02:53,600
And the reason I'm using guard let is because even though we know there's a value it's still an optional

47
00:02:53,600 --> 00:02:54,300
property.

48
00:02:54,320 --> 00:02:59,780
So we're using a guard led to basically create a constant if there is a value and otherwise we'll just

49
00:02:59,780 --> 00:03:04,140
return if there's no value so that's good because that will avoid a crash.

50
00:03:04,250 --> 00:03:10,850
Then we use it from string to force cast the string here as an int and that's cool because normally

51
00:03:10,850 --> 00:03:13,990
this is an optional and you have to force unwrap it.

52
00:03:14,000 --> 00:03:15,350
But this is a safe way to do it.

53
00:03:15,350 --> 00:03:20,440
So if we can't if for some reason they typed in letters if they could somehow figure out a way like

54
00:03:20,450 --> 00:03:26,810
maybe they accidentally pasted some letters in this would just return and it wouldn't cause a crash.

55
00:03:26,810 --> 00:03:34,310
So below that we're going to go ahead and use our binary decimal class to create an instance and pass

56
00:03:34,310 --> 00:03:36,250
in our integer as a decimal.

57
00:03:36,380 --> 00:03:40,910
So go ahead and type let binary decimal.

58
00:03:40,910 --> 00:03:42,640
Well let's say let.

59
00:03:42,650 --> 00:03:44,420
Maybe binary digit.

60
00:03:44,420 --> 00:03:50,390
And we're going to use our class here binary decimal and we can call our initializers here we have two

61
00:03:50,390 --> 00:03:50,920
of them.

62
00:03:50,960 --> 00:03:53,320
One is decimal and one is bits.

63
00:03:53,360 --> 00:03:54,640
This one is going to be a decimal.

64
00:03:54,640 --> 00:03:56,360
So select decimal.

65
00:03:56,600 --> 00:04:00,780
OK delete it and we can pass in in from string.

66
00:04:00,800 --> 00:04:07,820
Now we can go ahead and actually set the value of our textfield to represent our new converted number

67
00:04:07,830 --> 00:04:13,540
so we can say value entery textfield dot text is equal to a string.

68
00:04:13,730 --> 00:04:20,080
And we want to actually use our binary digits so we can use something called String encapsulation K

69
00:04:20,570 --> 00:04:22,730
and we can pass in binary digit.

70
00:04:22,730 --> 00:04:29,450
But that's that's the instance of our class in order to actually get the binary value for the integer

71
00:04:29,450 --> 00:04:30,300
that we just set.

72
00:04:30,320 --> 00:04:37,120
We can call DOT calculate binary value for int and that'll print out the binary value.

73
00:04:37,220 --> 00:04:38,270
You want to try it out.

74
00:04:38,630 --> 00:04:39,660
Let's do it.

75
00:04:39,670 --> 00:04:40,340
BuildOn run.

76
00:04:40,340 --> 00:04:41,900
Let's go see how it works.

77
00:04:42,230 --> 00:04:48,120
And then we're going to go ahead and move on to this decimal Iby action.

78
00:04:48,140 --> 00:04:54,930
OK so we enter in a value let's try well 145 that's been the number that we've been using.

79
00:04:54,980 --> 00:05:00,610
So we have a value when we tap the button the decimal button should go down to 0.5.

80
00:05:00,680 --> 00:05:06,380
We should basically calculate the binary value for this decimal and then replace the value entry textfield

81
00:05:06,380 --> 00:05:09,020
text with the binary representation.

82
00:05:09,020 --> 00:05:11,000
So click binary.

83
00:05:11,750 --> 00:05:12,420
Look at that.

84
00:05:12,470 --> 00:05:14,000
That works that's exactly right.

85
00:05:14,030 --> 00:05:16,580
1 0 0 1 0 0 0 or 1.

86
00:05:16,580 --> 00:05:17,570
That's exactly right.

87
00:05:17,570 --> 00:05:18,850
That's what we need.

88
00:05:18,860 --> 00:05:23,270
Now we're going to write the Iby action for the decimal button so that it can convert it back into a

89
00:05:23,270 --> 00:05:23,850
decimal.

90
00:05:23,930 --> 00:05:24,210
OK.

91
00:05:24,260 --> 00:05:25,250
So let's do it.

92
00:05:25,250 --> 00:05:26,700
Very very cool stuff though.

93
00:05:27,050 --> 00:05:32,120
So we're going to use the same validation to make sure we have a value and so to do that we're going

94
00:05:32,120 --> 00:05:38,890
to type if value entry textfield dot text is not equal to nothing.

95
00:05:38,930 --> 00:05:42,860
Meaning if there's something there we're going to go ahead and do the opposite of above.

96
00:05:42,860 --> 00:05:46,940
We're going to set binary button 2.5 and decimal button to 1.0.

97
00:05:46,940 --> 00:05:57,470
So binary buttons Alpha is equal to zero point five and decimal button Alpha is one point zero.

98
00:05:57,560 --> 00:06:00,760
Then we're going to do something similar to what we did before.

99
00:06:00,770 --> 00:06:06,620
We're going to use the Guard letter to create a string for the value entry textfield.

100
00:06:07,040 --> 00:06:13,190
But basically what we're going to need to do is we're going to need to take each individual item from

101
00:06:13,250 --> 00:06:20,590
our string K from the string from the value into textfield and convert it into an array of integers

102
00:06:20,840 --> 00:06:26,630
k because remember the other way we can create a binary decimal is from an array of integers of ones

103
00:06:26,630 --> 00:06:27,600
and zeros.

104
00:06:27,620 --> 00:06:29,560
So let's do that now.

105
00:06:29,570 --> 00:06:31,630
So type guard let.

106
00:06:31,640 --> 00:06:35,620
String equals value entery textfield dot text.

107
00:06:35,660 --> 00:06:38,870
Else we're going to go ahead and just return.

108
00:06:38,870 --> 00:06:39,690
OK.

109
00:06:39,950 --> 00:06:45,470
Now we're going to use that string to essentially turn it into an array of integers and we can use the

110
00:06:45,470 --> 00:06:47,390
map function for that's very helpful.

111
00:06:47,390 --> 00:06:55,540
So go ahead and type let bits from string equals string dot map.

112
00:06:55,730 --> 00:07:00,570
And as you can see we can actually transform each character just like we did before.

113
00:07:00,920 --> 00:07:03,850
And we can actually get rid of these parentheses here you don't need them.

114
00:07:04,130 --> 00:07:11,450
But in the curly brackets inside the closure what we can do is we can take the dollar sign zero value

115
00:07:11,450 --> 00:07:18,100
meaning every character of the string which if we type in binary would be you know 1 0 1 1 0.

116
00:07:18,110 --> 00:07:19,510
It would be a 1 or 0.

117
00:07:19,700 --> 00:07:29,420
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

118
00:07:29,420 --> 00:07:35,300
sure that it's a string coming in and what we're going to do is we're going to encapsulate that as an

119
00:07:35,360 --> 00:07:36,010
end.

120
00:07:36,090 --> 00:07:39,310
OK we're going to force cast it to be a forecastable.

121
00:07:39,320 --> 00:07:42,180
We're casting it as an integer.

122
00:07:42,200 --> 00:07:45,970
Now if I try to build this it's going to say hey error can't be actually done.

123
00:07:45,980 --> 00:07:52,910
We have to actually force on Rapide here to turn it into an integer k because remember a string could

124
00:07:52,910 --> 00:07:54,230
be an integer or it could not.

125
00:07:54,230 --> 00:07:55,420
And that's why it's optional.

126
00:07:55,460 --> 00:08:02,240
So we're going to unwrap it there and make sure that it is in fact an optional urn or make sure that

127
00:08:02,240 --> 00:08:05,180
it is in fact a number so we can actually cast it.

128
00:08:05,180 --> 00:08:11,480
Now the interesting thing is is that map is going to return an array of whatever value we go through

129
00:08:11,490 --> 00:08:14,450
so we're cycling through every character in the string.

130
00:08:14,450 --> 00:08:17,030
What we're doing is we're converting them into an integer.

131
00:08:17,060 --> 00:08:22,130
So now we have an array of integers ones and zeros and then what we're going to do is we're going to

132
00:08:22,130 --> 00:08:24,890
create an instance of binary decimal.

133
00:08:24,920 --> 00:08:30,770
So go ahead and type let binary digit equals binary decimal and check it out.

134
00:08:30,770 --> 00:08:38,870
I can call the constructor here and we can pass in our array of bits which is bits from string and that

135
00:08:38,870 --> 00:08:45,170
will allow us to create what we need now that we have now that we've done this we can call our function

136
00:08:45,170 --> 00:08:48,570
that we just wrote calculate value for binary.

137
00:08:48,740 --> 00:08:54,020
So let's go ahead and set the value entery textfield text to be equal to a string.

138
00:08:54,020 --> 00:08:55,240
Just like above.

139
00:08:55,280 --> 00:09:02,870
Use string encapsulation to pass in the binary digit K from this function and we can call calculate

140
00:09:03,140 --> 00:09:05,030
value for binary.

141
00:09:05,030 --> 00:09:11,990
So let's go ahead let's build and run this let's see if what we did worked to convert our decimal numbers

142
00:09:12,080 --> 00:09:12,980
into binary.

143
00:09:12,980 --> 00:09:18,260
It should change the colors and the opacities of the buttons and it should successfully convert the

144
00:09:18,260 --> 00:09:18,560
value.

145
00:09:18,560 --> 00:09:21,510
So let's actually begin with a binary value.

146
00:09:21,770 --> 00:09:23,910
Let's go ahead and begin with.

147
00:09:23,940 --> 00:09:27,180
1 0 0 1 0 0 0 0 1.

148
00:09:27,290 --> 00:09:29,970
If you remember that should be 145.

149
00:09:30,140 --> 00:09:33,770
And we're going to convert this into a decimal when I push this.

150
00:09:33,800 --> 00:09:40,920
It should convert to 145 3 to 1 click and it works that's amazing.

151
00:09:40,920 --> 00:09:46,260
So since there's a decimal here now I should be able to click binary and switch between them automatically

152
00:09:46,650 --> 00:09:47,210
and look at that.

153
00:09:47,220 --> 00:09:48,310
It works.

154
00:09:48,330 --> 00:09:49,260
That's exactly right.

155
00:09:49,260 --> 00:09:50,730
Really really cool stuff.

156
00:09:50,790 --> 00:09:55,650
So we can now successfully convert decimal numbers all the way up to binary numbers.

157
00:09:55,650 --> 00:10:00,300
Now there are of course are many many ways that this app could be expanded and made better.

158
00:10:00,390 --> 00:10:04,620
You could make a you know a secret thought translator that you could type in words and convert them

159
00:10:04,620 --> 00:10:05,550
into binary.

160
00:10:05,550 --> 00:10:07,730
We'll get to that in the challenge video guys.

161
00:10:07,770 --> 00:10:08,730
Amazing work.

162
00:10:08,730 --> 00:10:13,680
Our app can successfully convert between binary and decimal numbers.

163
00:10:13,680 --> 00:10:14,880
Super super great work.

164
00:10:14,880 --> 00:10:19,230
Let's head over to the next video where you're going to have a quick challenge to make this app even

165
00:10:19,260 --> 00:10:20,130
cooler.

166
00:10:20,130 --> 00:10:20,890
I'll see you there.
