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