0
1
00:00:00,270 --> 00:00:03,540
In the last lesson, we started working with UISliders.
1

2
00:00:03,540 --> 00:00:07,840
In this lesson, we'll use the slider values to calculate the BMI.
2

3
00:00:07,890 --> 00:00:15,510
What if we wanted to get a hold of our slider values outside of these IBActions where we have access
3

4
00:00:15,600 --> 00:00:19,290
to the sender where we're getting that sender.value from,
4

5
00:00:19,350 --> 00:00:28,070
how might we do that? So if I go ahead and create IBAction from this calculate button and put
5

6
00:00:28,070 --> 00:00:31,160
it right at the bottom of my View Controller,
6

7
00:00:31,610 --> 00:00:43,560
so let's call that the calculatePressed, and change the type to UIButton, click Connect.
7

8
00:00:43,580 --> 00:00:50,220
Now, I've got this IBAction which gets triggered whenever the user taps on the calculate button.
8

9
00:00:50,240 --> 00:00:58,190
Now, how can we get hold of the weight and height inside these curly braces? Because this sender is, of
9

10
00:00:58,190 --> 00:01:05,450
course, the button that triggered the IBAction, whereas this sender is the slider that triggered this
10

11
00:01:05,450 --> 00:01:13,190
IBAction, and they are separate from each other. One way of doing this, which is probably the easiest
11

12
00:01:13,190 --> 00:01:18,050
way, is to simply create some IBOutlets from these sliders as well.
12

13
00:01:18,140 --> 00:01:26,690
We've got our IBActions which gets triggered when these are moved, but we can also create a IBOutlet
13

14
00:01:26,750 --> 00:01:35,570
from the height slider which I'll simply just call heightSlider, and another IBOutlet from our weight
14

15
00:01:35,570 --> 00:01:44,390
slider which is gonna be called weightSlider. And now because we have these variables to play with, our
15

16
00:01:44,390 --> 00:01:51,130
heightSlider and our weightSlider which are linked to the Interface Builder Sliders. Well, inside our
16

17
00:01:51,140 --> 00:02:02,390
calculatePressed, we could quite easily get hold of the heighSlider.value and the weightSlider.value
17

18
00:02:03,330 --> 00:02:12,400
And if we run our app, you can see that when I move these sliders and click calculate, you
18

19
00:02:12,400 --> 00:02:18,920
can see the original floating point values from each of these sliders being printed.
19

20
00:02:19,210 --> 00:02:28,780
So now that we have the height and the weight, let's set this to a height constant, and down here, let's
20

21
00:02:28,780 --> 00:02:33,500
create a a weight constant.
21

22
00:02:34,160 --> 00:02:43,390
How can we use these two pieces of data and calculate the body mass index? Here's the equation for calculating
22

23
00:02:43,420 --> 00:02:46,180
the body mass index or BMI.
23

24
00:02:46,210 --> 00:02:55,520
We take the weight in kilograms and we divide it by the height in meters squared, so height times height.
24

25
00:02:55,510 --> 00:03:01,810
Now, I want you to try and be able to print out the BMI based on the weight and height that we got from
25

26
00:03:01,810 --> 00:03:05,550
the sliders, and calculate it and print it into the console
26

27
00:03:05,590 --> 00:03:09,820
based on this equation. So pause the video and try to complete this challenge.
27

28
00:03:12,950 --> 00:03:15,900
Okay, so we have our height and we have our weight,
28

29
00:03:15,920 --> 00:03:20,810
we just need to implement the equation, so that we can create the BMI.
29

30
00:03:21,140 --> 00:03:28,430
So I'm going to create a constant called BMI and it's going to be equal to the weight divided by the
30

31
00:03:28,430 --> 00:03:30,190
height squared.
31

32
00:03:30,200 --> 00:03:32,520
Now, there's two ways of doing this.
32

33
00:03:32,540 --> 00:03:39,110
You can either do the simple way where we just write height times height because after all, that is what
33

34
00:03:39,170 --> 00:03:42,060
height squared does at the end of the day.
34

35
00:03:42,590 --> 00:03:47,870
But the other way that you could do this is use a custom Swift method.
35

36
00:03:47,990 --> 00:03:49,540
So you might have come across this
36

37
00:03:49,550 --> 00:03:55,700
if you Google for "How to square a number using Swift." But there is a function called the power function
37

38
00:03:56,210 --> 00:04:02,300
which takes two inputs. The first, the number that you want to raise to a particular power.
38

39
00:04:02,720 --> 00:04:04,940
And the second, the actual exponent.
39

40
00:04:04,970 --> 00:04:09,950
So in this case, if we wanted to get the height squared, we would write "pow,"
40

41
00:04:10,010 --> 00:04:15,760
and then the first input would be the height which is the value we want to raise to a power,
41

42
00:04:16,280 --> 00:04:21,750
and then the second value is the power that we want to raise it to which is 2.
42

43
00:04:21,770 --> 00:04:30,140
So this line of code is effectively exactly the same as this part of our equation where we raise our
43

44
00:04:30,140 --> 00:04:41,760
height to the power of 2. And now we can print our BMI into the console. And you can see, if I put in my
44

45
00:04:41,760 --> 00:04:48,170
height: 1.8, and my weight: 63, and hit calculate,
45

46
00:04:48,480 --> 00:04:51,940
you can see my body mass index printed here.
46

47
00:04:52,260 --> 00:04:58,740
The thing that you have to be careful about is that in programming, as with maths, it follows something
47

48
00:04:58,740 --> 00:05:04,600
called BODMAS which defines the order that the calculations are completed.
48

49
00:05:04,620 --> 00:05:09,780
So the first thing that happens are anything that's in a bracket and then anything that's an exponent
49

50
00:05:09,810 --> 00:05:10,990
or a root,
50

51
00:05:11,160 --> 00:05:13,880
and then we have our division or multiplication,
51

52
00:05:13,920 --> 00:05:22,050
and finally, our addition or subtraction. So you might have written the code as a weight divided by height
52

53
00:05:22,380 --> 00:05:24,240
times height.
53

54
00:05:24,240 --> 00:05:32,220
Now, in this case, what happens is that the first calculation: weight divided by height, actually gets carried
54

55
00:05:32,220 --> 00:05:36,820
out first, instead of what we need which is the height squared,
55

56
00:05:36,840 --> 00:05:46,370
so height times height. And if you run the code as it is right now, you'll see that if I put in my height
56

57
00:05:46,400 --> 00:05:47,270
and weight again,
57

58
00:05:51,070 --> 00:05:58,420
instead of getting something around 19, I'm actually getting 62 which is extremely, morbidly obese
58

59
00:05:58,990 --> 00:06:04,850
which would shock somebody if they used your app that was using this incorrect equation.
59

60
00:06:04,930 --> 00:06:10,270
So we have to put in those brackets around the height times height if we want to write our equation
60

61
00:06:10,270 --> 00:06:18,890
like this, so that the brackets are carried out first before any of the other operations. Now that we've
61

62
00:06:18,980 --> 00:06:26,270
managed to get our slider to work to be able to put input into the slider, to change and update our label,
62

63
00:06:26,660 --> 00:06:29,070
and use it to calculate the BMI,
63

64
00:06:29,300 --> 00:06:32,000
well, what else is there left in this module?
64

65
00:06:32,000 --> 00:06:36,380
Don't worry there's still plenty of things coming up. And in the next lesson, we're going to learn about
65

66
00:06:36,440 --> 00:06:41,540
a really exciting topic which are classes. For all of that and more,
66

67
00:06:41,570 --> 00:06:42,260
I'll see there.
