0
1
00:00:00,610 --> 00:00:04,870
In this lesson, we're going to add the finishing touches to our BMI Calculator.
1

2
00:00:04,900 --> 00:00:10,960
We're going to apply the MVC design pattern to add some additional functionality to our app. By the end
2

3
00:00:10,960 --> 00:00:11,770
of this lesson,
3

4
00:00:11,790 --> 00:00:19,000
our Apple display a piece of advice text underneath the BMI value, and also show a different background
4

5
00:00:19,000 --> 00:00:25,930
color depending on whether if the BMI that was calculated happens to be underweight or overweight.
5

6
00:00:25,930 --> 00:00:32,230
In the process, we'll learn how to work with color laterals and also get more practice working with optionals
6

7
00:00:32,260 --> 00:00:33,430
in Swift.
7

8
00:00:33,430 --> 00:00:40,180
Speaking of optionals, prior to our Optionals Deep Dive, we were looking at our BMI property in our struct.
8

9
00:00:40,180 --> 00:00:46,270
We transformed this property into an optional and we were wondering how to best work with it in the
9

10
00:00:46,270 --> 00:00:51,930
getBMIValue function. So coming back to our projects,
10

11
00:00:51,950 --> 00:00:57,830
having learned about all the ways that we can handle optional, let's think about how we can apply those
11

12
00:00:57,830 --> 00:01:02,270
methods that we learned about to our optional value here.
12

13
00:01:02,390 --> 00:01:09,210
The first thing that we learned about is we could simply do a "if BMI is not equal to nil,"
13

14
00:01:09,340 --> 00:01:14,790
well then in that case, and only in that case, do we actually trigger this block.
14

15
00:01:14,900 --> 00:01:24,320
Now, once we reindent our code, and then we go ahead and unwrap this bmiVvalue safely within the check
15

16
00:01:24,440 --> 00:01:31,280
of our "if" statement. Now, that error goes away, but we get a new error. It's telling us that we're missing
16

17
00:01:31,280 --> 00:01:37,280
a return in a function expected to return "String." Because, yeah, when we created this function 
17

18
00:01:37,280 --> 00:01:43,150
getBMIValue, we promised that we would give a string as an output by the end of this function.
18

19
00:01:43,250 --> 00:01:49,970
But you can see that if the BMI was indeed nil, then this entire block of code is going to be skipped
19

20
00:01:50,440 --> 00:01:55,180
and we're gonna land right here without having any value to give back.
20

21
00:01:55,190 --> 00:01:56,150
So what can we do?
21

22
00:01:56,180 --> 00:02:03,070
Well, we could provide a "else" statement. So we could say that, well, in the case where BMI is nil, then let's
22

23
00:02:03,080 --> 00:02:05,480
simply just return "0.0."
23

24
00:02:05,990 --> 00:02:10,590
So that's one way of looking at it. The other way is using are optional binding,
24

25
00:02:10,610 --> 00:02:16,810
so where we say, if let safeBMI = bmi.
25

26
00:02:17,390 --> 00:02:24,110
Well, in that case, as long as this has a value, then we should be able to use this safeBMI as an actual
26

27
00:02:24,200 --> 00:02:25,810
floating point number.
27

28
00:02:25,820 --> 00:02:34,190
So here we don't have to do any unwrapping anywhere, and we can just simply tap into the safeBMI.
28

29
00:02:34,220 --> 00:02:41,690
But, again, in this case, we still need that "else" statement because if the BMI is nil, then, again, this block will
29

30
00:02:41,690 --> 00:02:46,130
be skipped and it will jump to the "else" statement.
30

31
00:02:46,460 --> 00:02:54,380
Now, if we wanted this code where we're using the BMI value if it has a value, otherwise, providing a default
31

32
00:02:54,410 --> 00:03:02,120
value, well, then this whole blogger code could actually be vastly simplified by simply deleting the "if"
32

33
00:03:02,360 --> 00:03:06,910
and the "else" statement, and using our nil coalescing operator.
33

34
00:03:06,920 --> 00:03:14,960
So in this case, we're going to say that, well, if there is a value in BMI, well, then use that value,
34

35
00:03:15,050 --> 00:03:22,830
but otherwise, let's just use 0.0 as the value that we format. So out of those three options,
35

36
00:03:22,880 --> 00:03:28,490
I would actually prefer this approach. The syntax with the nil coalescing operator is both more clear
36

37
00:03:28,580 --> 00:03:30,530
and more expressive.
37

38
00:03:30,530 --> 00:03:37,550
So now that we've figured out the bmiValue, the next step is getting hold of an interpretation for the
38

39
00:03:37,550 --> 00:03:43,270
bmiValue and assigning it a piece of advice as well as a color.
39

40
00:03:43,310 --> 00:03:51,920
Now in that case, we should probably end up grouping our bmiValue with its color and advice together
40

41
00:03:52,040 --> 00:03:54,080
in the same data type.
41

42
00:03:54,080 --> 00:03:56,810
So does that remind you of something?
42

43
00:03:56,810 --> 00:04:01,320
Well, it means that we should probably create a model for our BMI.
43

44
00:04:01,400 --> 00:04:01,940
That's right.
44

45
00:04:01,940 --> 00:04:06,140
We're going to build our app to be more in line with the MVC design pattern.
45

46
00:04:06,140 --> 00:04:14,770
So in our Models folder, let's go ahead and create a new file, and it's gonna be a Swift File, and let's
46

47
00:04:14,770 --> 00:04:24,130
go ahead and name it BMI, and make sure it goes into the Models folder. In this BMI file,
47

48
00:04:24,130 --> 00:04:31,090
I'm going to create a new struct called BMI, and it's simply going to have three properties: a value which
48

49
00:04:31,090 --> 00:04:37,860
is going to be a float, a piece of advice which is going to be a string,
49

50
00:04:38,200 --> 00:04:43,450
and finally, a color which is going to be a UIColor.
50

51
00:04:43,450 --> 00:04:47,590
Now, notice how it's not coming up in my autosuggest here.
51

52
00:04:47,590 --> 00:04:52,630
And that should be a good hint to you that we actually are using something that we don't currently have
52

53
00:04:52,630 --> 00:04:53,830
access to,
53

54
00:04:53,890 --> 00:05:02,320
and this says just as much because we need to switch our foundation to UIKit, so that the UIColor which
54

55
00:05:02,320 --> 00:05:06,620
comes from UIKit can be found, and we can use it.
55

56
00:05:06,640 --> 00:05:09,230
So it's a pretty simple struct for BMI.
56

57
00:05:09,310 --> 00:05:15,000
It's going to be a custom data type that grouped together the value which is the number,
57

58
00:05:15,160 --> 00:05:18,250
and then the advice text as well as the color.
58

59
00:05:18,280 --> 00:05:24,550
Now, we can head back into our CalculatorBrain. And instead of having our BMI being a float, I'm going
59

60
00:05:24,550 --> 00:05:29,870
to change its data type to being that new BMI struct that we created.
60

61
00:05:29,980 --> 00:05:32,820
But again, it's going to be an optional.
61

62
00:05:32,860 --> 00:05:38,710
So now that we've changed the data type, we have to do a few fixes in our code.
62

63
00:05:38,710 --> 00:05:45,320
And the first thing is that we should change this which is no longer a value or a floating point number,
63

64
00:05:45,400 --> 00:05:47,460
it's now a BMI struct,
64

65
00:05:47,710 --> 00:05:55,320
instead it should become a bmi.value, so that we can tap into that floating point number.
65

66
00:05:55,750 --> 00:06:02,140
But notice how as soon as I type the word "value," what gets automatically inserted for me is that question
66

67
00:06:02,140 --> 00:06:08,320
mark there. And from the last lesson when we did our Deep Dive into optionals, you should be able to recognize
67

68
00:06:08,320 --> 00:06:12,940
that this is effectively changing our BMI optional.
68

69
00:06:13,030 --> 00:06:17,490
So because the value is a normal floating point number,
69

70
00:06:17,890 --> 00:06:21,010
but the BMI is, of course, the optional here,
70

71
00:06:21,010 --> 00:06:26,500
in order to reach into the BMI to get a hold of that value, we first have to check to make sure that the
71

72
00:06:26,500 --> 00:06:30,120
BMI is not nil like it is here.
72

73
00:06:30,280 --> 00:06:35,110
And if it is indeed not nil, well, then we can get hold of the value that lives inside.
73

74
00:06:35,110 --> 00:06:40,270
But if it is nil, then we're simply going to use "0.0" as of value.
74

75
00:06:43,020 --> 00:06:45,720
So, that's our first part fixed.
75

76
00:06:45,720 --> 00:06:52,980
Now down here, though, we can't simply just write BMI value and use optional chaining to try to assign
76

77
00:06:52,980 --> 00:07:01,830
this to our value property in the BMI struct. Because remember that the value was set as a "let" constant.
77

78
00:07:02,370 --> 00:07:08,860
Once we create this BMI with the value, advice, and color, all we want to do is just pass it around.
78

79
00:07:08,880 --> 00:07:10,830
We don't want to change it.
79

80
00:07:10,890 --> 00:07:13,560
It's only ever going to have one value.
80

81
00:07:13,560 --> 00:07:20,850
And so that means what we actually want is to be able to create a BMI struct from scratch.
81

82
00:07:20,850 --> 00:07:29,160
So instead of writing bmi.value, let's create a temporary constant. Let's call it let bmiValue = height,
82

83
00:07:29,160 --> 00:07:39,030
and then we can create our bmi by using that BMI struct, and then using the initializers to fill
83

84
00:07:39,030 --> 00:07:40,550
in each of these values.
84

85
00:07:40,590 --> 00:07:46,920
So the BMI value that we calculated, of course, goes into the value placeholder, and then we need a piece
85

86
00:07:46,920 --> 00:07:55,340
of advice and a piece of color, but these two things are worked out based on what value we got. Because
86

87
00:07:55,340 --> 00:08:01,760
the reason why the BMI is such a useful indicator for health professionals is because it takes into
87

88
00:08:01,760 --> 00:08:07,520
account not just somebody's weight, but also their height. A really tall person has the same weight as
88

89
00:08:07,520 --> 00:08:12,640
somebody who's really short, then the shorter person probably is more overweight.
89

90
00:08:12,770 --> 00:08:19,400
So in the BMI chart, you can see these different bands which classify the BMI values, where below 18.5,
90

91
00:08:19,400 --> 00:08:25,610
they are underway between 18.5 and 24.9, they are healthy.
91

92
00:08:26,150 --> 00:08:30,340
And then above that, they are overweight, obese, and extremely obese.
92

93
00:08:30,530 --> 00:08:36,890
The National Institute of Health classify some of these categories and we're going to use these categories
93

94
00:08:36,890 --> 00:08:39,490
to create an "if" statement in our code.
94

95
00:08:39,740 --> 00:08:47,060
So as a challenge, I want you to look at the BMI value that we calculated from dividing the height squared
95

96
00:08:47,090 --> 00:08:53,720
by the weight, and then I want you to print underweight, normal, or overweight based on what that value
96

97
00:08:53,720 --> 00:08:56,150
is equal to. For this challenge,
97

98
00:08:56,150 --> 00:08:58,100
just print to the console for now.
98

99
00:08:58,100 --> 00:09:03,230
We'll be adding the functionality to show the interpretation inside the app in a bit.
99

100
00:09:03,230 --> 00:09:04,820
So take a look at this breakdown.
100

101
00:09:05,090 --> 00:09:07,760
Pause the video and try to complete this challenge
101

102
00:09:10,760 --> 00:09:12,670
All right, so let's first comment out
102

103
00:09:12,680 --> 00:09:16,030
this line of code which we don't have any values for,
103

104
00:09:16,100 --> 00:09:22,250
and then we're going to use our bmiValue which gets calculated, and we're going to check to see if the
104

105
00:09:22,310 --> 00:09:26,840
bmiValue is less than 18.5.
105

106
00:09:26,840 --> 00:09:29,960
Well, in that case, then we should print "underweight."
106

107
00:09:34,100 --> 00:09:41,000
But the next category for the normal weight is for people who are between 18.5
107

108
00:09:41,000 --> 00:09:41,930
and 24.9.
108

109
00:09:41,930 --> 00:09:44,060
So how can we express that range?
109

110
00:09:44,060 --> 00:09:51,290
Well, we could use an "else if" which only gets triggered, remember, if the "if" statement, this check, fails.
110

111
00:09:51,710 --> 00:09:58,550
Well, in that case, if it's not less than 18.5, well, then, is the bmiValue less than
111

112
00:09:58,550 --> 00:10:00,410
24.9.
112

113
00:10:00,420 --> 00:10:08,620
Well, in this case, we should be printing "normal weight." And finally, if the BMI value is not less than
113

114
00:10:08,620 --> 00:10:13,990
18.5, and is not less than 24.9, well, then we can catch everything else
114

115
00:10:14,050 --> 00:10:20,440
above 24.9 with the "else" statement, and we can print "overweight."
115

116
00:10:20,710 --> 00:10:23,130
So this is one solution to our problem.
116

117
00:10:23,140 --> 00:10:29,740
Now, you might have done it slightly differently. You might have checked to see if the BMI value was greater
117

118
00:10:29,740 --> 00:10:37,810
than 18.5, and it was less than 24.9 to catch that particular range,
118

119
00:10:37,810 --> 00:10:43,510
and that would be the case if you only had an if statement. But if you have an "else" statement, remember that
119

120
00:10:43,540 --> 00:10:49,630
it's already checked the previous condition, it's already checked and made sure that the bmiValue is
120

121
00:10:49,630 --> 00:10:52,130
not less than 18.5.
121

122
00:10:52,300 --> 00:10:55,850
And that's the point at which the "else" statement actually gets triggered,
122

123
00:10:55,900 --> 00:11:00,610
so we don't actually have to check the lower end, we just have to check the upper bound which is
123

124
00:11:00,610 --> 00:11:05,110
24.9. And that's the solution to the challenge.
124

125
00:11:05,220 --> 00:11:07,910
Now, let's make use of this logic.
125

126
00:11:08,010 --> 00:11:16,980
So now instead of printing "underweight," "normal weight," and "overweight," let's instead create our BMI struct
126

127
00:11:17,310 --> 00:11:19,950
when we have these different categories.
127

128
00:11:20,010 --> 00:11:27,060
So if the bmiValue was less than 18.5, well, then maybe the advice is we should tell them
128

129
00:11:27,060 --> 00:11:36,930
to "Eat more pies," and the color that we will use to show that they're underweight is maybe we could just
129

130
00:11:36,930 --> 00:11:40,130
use the UIColor.blue.
130

131
00:11:40,230 --> 00:11:47,460
Now, notice how UIColor, again, comes from UIKit. And without importing UIKit, we won't be able
131

132
00:11:47,460 --> 00:11:49,120
to use it.
132

133
00:11:49,230 --> 00:11:54,240
But there's another way that we could do this by using something called a Color Literal and we're not
133

134
00:11:54,240 --> 00:11:56,230
going to use UIColor anymore.
134

135
00:11:56,250 --> 00:12:02,550
Instead, we're going to start typing Color Literal. And once that hint comes up, we're going to hit enter.
135

136
00:12:02,550 --> 00:12:08,790
And now in this little square, we can double click on it and out pops a little color palette that we
136

137
00:12:08,790 --> 00:12:09,880
can pick from.
137

138
00:12:09,900 --> 00:12:15,510
Now, if these colors aren't enough for you, then you can click on Other, and you get the full choice of
138

139
00:12:15,530 --> 00:12:20,100
RGB Sliders or the rainbow, or the crayons, whichever one you prefer.
139

140
00:12:20,550 --> 00:12:26,370
But in my case, I'm simply just going to choose this light blue color for people who are underweight.
140

141
00:12:27,150 --> 00:12:36,720
And then for normal weight, I'm going to say, maybe, "Fit as a fiddle!" and let's add an exclamation mark
141

142
00:12:36,720 --> 00:12:39,430
here as well to express our enthusiasm.
142

143
00:12:39,720 --> 00:12:42,870
And let me just fix my typo there.
143

144
00:12:42,920 --> 00:12:49,930
Now, let's change this color to green for normal weight. And finally, for overweight,
144

145
00:12:50,010 --> 00:12:58,560
we're going to say, maybe, "Eat less pies!" because that's what everybody eats, just pies all day.
145

146
00:12:58,560 --> 00:13:01,930
And I'm going to change the color to red. All right.
146

147
00:13:02,080 --> 00:13:10,790
So I've got blue, green, red, and I've got a piece of advice for everybody from each category. And now we've
147

148
00:13:10,790 --> 00:13:14,670
calculated the BMI based on the values that were passed in.
148

149
00:13:14,780 --> 00:13:20,670
We've interpreted them and created a separate BMI struct for each value category.
149

150
00:13:20,750 --> 00:13:25,560
So let's tie everything that you've learned so far together with a challenge.
150

151
00:13:25,730 --> 00:13:32,690
Head back over to the CalculateViewController. And down here where we have access to this destinationVC,
151

152
00:13:32,700 --> 00:13:37,320
which is the ResultViewController that's going to be shown up on screen,
152

153
00:13:37,370 --> 00:13:40,340
I want you to type two more lines of code.
153

154
00:13:40,340 --> 00:13:46,910
So we're going to tap into the destinationVC and we're going to set a property called advice,
154

155
00:13:46,970 --> 00:13:56,780
and we're going to set it equal to the calculatorBrain.getadvice. And then we're also going to tap
155

156
00:13:56,780 --> 00:14:03,110
into the destinationVC, and set another property codecolor which is going to be coming from the
156

157
00:14:03,170 --> 00:14:05,380
calculatorBrain as always.
157

158
00:14:05,540 --> 00:14:07,780
And it's going to be called getColor.
158

159
00:14:08,360 --> 00:14:17,000
So using what you've learned about optionals, about MVC, and struct, I want you to complete this challenge
159

160
00:14:17,240 --> 00:14:19,680
by getting rid of the errors here.
160

161
00:14:19,680 --> 00:14:25,220
Now, you're not allowed to touch anything else inside the CalculateViewController and you have to fix
161

162
00:14:25,220 --> 00:14:32,540
this code and make it work, so that when you run the app and you change the height to some extraordinary
162

163
00:14:32,540 --> 00:14:42,200
number and click calculate, it should calculate a low BMI which means a blue background, which means eat
163

164
00:14:42,200 --> 00:14:43,260
more pies.
164

165
00:14:43,580 --> 00:14:49,460
And if you had a normal-ish sort of height and a normal-ish sort of weight,
165

166
00:14:52,720 --> 00:14:58,360
then you should end up with a green background with a "Fit as a fiddle" as the advice text.
166

167
00:14:58,360 --> 00:15:06,550
And finally, if you have a really low height and a normal weight, then you might get a higher BMI result
167

168
00:15:06,880 --> 00:15:09,520
with a different advice and a red background.
168

169
00:15:09,880 --> 00:15:16,030
So, essentially, these two lines of code will allow us to pass on the advice that we get out of our 
169

170
00:15:16,030 --> 00:15:22,850
calculatorBrain, the color that we got out of our calculatorBrain, and make it display in the ResultViewController.
170

171
00:15:22,900 --> 00:15:27,730
So there's a couple of things that you have to fix and you have to write, and it requires all of your
171

172
00:15:27,730 --> 00:15:29,560
knowledge that you've learned so far.
172

173
00:15:29,560 --> 00:15:31,930
Pause the video and try to complete this challenge.
173

174
00:15:36,910 --> 00:15:37,180
All right.
174

175
00:15:37,210 --> 00:15:40,030
So there's a few ways that you could have solved this challenge.
175

176
00:15:40,030 --> 00:15:45,760
But first of all, we have to get rid of these errors. And through the code that I've written, it requires
176

177
00:15:45,760 --> 00:15:51,750
that we have a property code advice and a property code color in our destination VC.
177

178
00:15:51,790 --> 00:15:55,510
So that is, of course, going to be our ResultViewController.
178

179
00:15:55,510 --> 00:16:01,660
So here we've already got a bmiValue, which is a optional string, because when we create the result VC,
179

180
00:16:01,720 --> 00:16:03,580
we don't yet know these values.
180

181
00:16:03,670 --> 00:16:09,280
We have to pass it over because the segue creates that view controller, at which point, we don't know
181

182
00:16:09,280 --> 00:16:15,520
any of these values and it's only when we cast it down to a ResultViewController, and then we can set
182

183
00:16:15,520 --> 00:16:16,470
its properties.
183

184
00:16:16,510 --> 00:16:19,180
So we need to create advice and color over here.
184

185
00:16:19,660 --> 00:16:25,480
So I'm going to create a variable code advice and this is also going to be an optional string, and
185

186
00:16:25,480 --> 00:16:30,770
then I'm going to create a variable code color which is going to be a optional UIColor.
186

187
00:16:31,780 --> 00:16:37,790
So now that should get rid of these two errors and that's no longer highlighted in red.
187

188
00:16:37,780 --> 00:16:42,830
So let's go ahead and address the next part, our getAdvice and getColor methods.
188

189
00:16:42,910 --> 00:16:47,020
So we currently don't yet have those things in our CalculatorBrain.
189

190
00:16:47,020 --> 00:16:48,650
So let's go ahead and create it.
190

191
00:16:48,680 --> 00:16:55,840
Let's create a function called getAdvice and this should probably return a String because the advice
191

192
00:16:55,840 --> 00:17:06,390
text is a string, and then let's create a function called getColor, and this will return a UIColor. So if
192

193
00:17:06,390 --> 00:17:11,180
you haven't converted your foundation into a UIKit, then you'll have to do that now in order for Xcode
193

194
00:17:11,190 --> 00:17:13,590
to know about this data type.
194

195
00:17:13,650 --> 00:17:18,360
So now it's saying that we're missing a return in a function expected to return.
195

196
00:17:18,360 --> 00:17:23,290
So in this case, we need to get hold of the BMI's advice property.
196

197
00:17:23,310 --> 00:17:27,930
So let's go ahead and try to return bmi.advice.
197

198
00:17:28,080 --> 00:17:33,620
Now, it automatically chains our BMI which currently is still a optional,
198

199
00:17:33,720 --> 00:17:40,890
and then it tries to tap into the advice property as long as it exists. But, of course, if BMI was nil,
199

200
00:17:41,220 --> 00:17:43,390
then advice would also be nil,
200

201
00:17:43,410 --> 00:17:48,700
so therefore, this entire bit of code is actually still an optional string.
201

202
00:17:48,750 --> 00:17:51,540
So how do we turn it into an actual string?
202

203
00:17:51,540 --> 00:17:54,250
Well, we could use our optional binding.
203

204
00:17:54,360 --> 00:17:59,180
We could use our nil checking, or more simply, and to keep it short and sweet,
204

205
00:17:59,180 --> 00:18:05,670
we could also just give it a default value of "No advice" in the case where our BMI is actually nil.
205

206
00:18:05,800 --> 00:18:11,910
Now, in pretty much all circumstances, this would never get triggered because our prepare for segue is
206

207
00:18:11,910 --> 00:18:17,910
always going to be triggered after perform Segway which, of course, lives in the calculatePressed IBAction
207

208
00:18:18,240 --> 00:18:20,520
which already calls calculateBMI.
208

209
00:18:20,520 --> 00:18:25,380
Now, we know this but our code doesn't know, and there might be a case in the future where we've forgotten
209

210
00:18:25,380 --> 00:18:26,250
all about this.
210

211
00:18:26,280 --> 00:18:31,950
So to catch that optional being nil, we provide a default value which should alert us that something
211

212
00:18:31,950 --> 00:18:33,060
has gone wrong.
212

213
00:18:33,210 --> 00:18:36,090
So let's go ahead and do the same in our getColor.
213

214
00:18:36,140 --> 00:18:42,960
So I'm going to return BMI chained with the .ccolor property. In this case, I'm just going to provide
214

215
00:18:42,990 --> 00:18:44,080
a default color,
215

216
00:18:44,100 --> 00:18:50,280
say, UIColor.white, or if you want to keep it consistent with the rest of these, then it might
216

217
00:18:50,280 --> 00:18:55,710
be a case of turning this into a Color Literal. But, of course, when there are errors, the Color Literal
217

218
00:18:55,710 --> 00:18:58,020
doesn't really pop up very easily in Xcode.
218

219
00:18:58,050 --> 00:19:02,360
So I find it's easier to just copy it from somewhere else and paste it in,
219

220
00:19:02,370 --> 00:19:06,400
and then we can double click on it to change it to white.
220

221
00:19:06,620 --> 00:19:06,890
All right.
221

222
00:19:06,890 --> 00:19:13,790
So now that we've created our getAdvice and getColor methods, if we go back to our CalculateViewController
222

223
00:19:13,910 --> 00:19:22,000
and hit command B to hit our code with its stick, then that should get rid of all of those errors.
223

224
00:19:22,000 --> 00:19:28,240
But now you might realize that if we run our app, it's still not enough to get the advice and color being
224

225
00:19:28,240 --> 00:19:34,330
displayed on screen because while we have access to them in these properties now, we're not using them
225

226
00:19:34,330 --> 00:19:34,930
yet.
226

227
00:19:34,960 --> 00:19:41,530
So in our viewDidLoad, in addition to setting the bmiLabel also gonna set the adviceLabel's text property
227

228
00:19:41,890 --> 00:19:45,290
to equal the advice string that we passed over.
228

229
00:19:45,510 --> 00:19:48,460
And notice how I'm not unwrapping these optionals,
229

230
00:19:48,580 --> 00:19:55,870
and the reason is because the bmiLabel.text property is actually expecting an optional string because
230

231
00:19:55,870 --> 00:19:56,830
it can be nil.
231

232
00:19:56,830 --> 00:19:59,810
You could have a label with no text and that's perfectly fine.
232

233
00:19:59,830 --> 00:20:01,770
The label can handle that.
233

234
00:20:01,780 --> 00:20:06,690
So in this case, these two data types match and that's why we're not getting any errors.
234

235
00:20:06,850 --> 00:20:09,010
But how do we set the background color?
235

236
00:20:09,010 --> 00:20:16,450
Well, remember that in the UIViewController, we have a property called view, and that view, of course,
236

237
00:20:16,510 --> 00:20:23,890
comes from the UIViewController. And it refers to this view that we see right here which is, of course,
237

238
00:20:23,890 --> 00:20:26,700
the background of this screen.
238

239
00:20:26,710 --> 00:20:31,210
Well, that sounds pretty much like what we need to change in order to change the background. And notice
239

240
00:20:31,210 --> 00:20:35,370
how at the moment we've got this blue background showing up,
240

241
00:20:35,380 --> 00:20:40,660
but it could also be yellow or it could be red, or it could be any other color, basically.
241

242
00:20:41,080 --> 00:20:48,130
So we're going to tap into this view which we always have access to whenever we create a new view controller
242

243
00:20:48,450 --> 00:20:53,070
and it's already linked to our code with a property called a view by default.
243

244
00:20:53,080 --> 00:20:59,670
So every single time you drag on a new UIViewController from the object library, and you put it onto
244

245
00:20:59,680 --> 00:21:06,880
the screen, and you link it up with a UIViewController class, then you should have access to that view
245

246
00:21:06,880 --> 00:21:10,150
property. And that will determine what the background looks like.
246

247
00:21:10,660 --> 00:21:17,500
So let's go ahead and change the view.background color property, and we could set it to the color
247

248
00:21:17,530 --> 00:21:19,330
that was passed over.
248

249
00:21:19,330 --> 00:21:27,080
So now let's go ahead and run our code. And you should see that when we change our weight and height,
249

250
00:21:27,410 --> 00:21:33,050
our BMI gets calculated. And depending on the result, we get a different interpretation and a different
250

251
00:21:33,080 --> 00:21:33,760
background.
251

252
00:21:33,980 --> 00:21:39,770
So have a mess around with that and see what you get. And if you didn't manage to complete the challenge
252

253
00:21:39,770 --> 00:21:47,180
in its entirety, be sure to review your code and try to fix it, so that it works. And if optionals were confusing,
253

254
00:21:47,210 --> 00:21:52,910
then take a look at the Optionals Deep Dive. If this view property tripped you up, then be sure to
254

255
00:21:52,910 --> 00:21:57,140
take a look at the lessons where we talked about classes and inheritance.
255

256
00:21:57,140 --> 00:22:01,070
Now, that's it for this module. And I hope to see you on the next one.
