Reputation: 12051
So I have this code to change the values of UILabel
based on the value of UISlider
:
- (void)slider03Changed:(UISlider *)slider{
int value = 10;
if (slider.value >= 0.1){
value = 20;
}
if (slider.value >= 0.2){
value = 30;
}
if (slider.value >= 0.3){
value = 40;
}
if (slider.value >= 0.4){
value = 50;
}
if (slider.value >= 0.5){
value = 60;
}
if (slider.value >= 0.6){
value = 70;
}
if (slider.value >= 0.7){
value = 80;
}
if (slider.value >= 0.8){
value = 90;
}
if (slider.value >= 0.9){
value = 100;
}
lbl01.text = [NSString stringWithFormat:@"%u hours", value];
}
Everything works fine, but I'm worried about the quality of code here.
This code is quite, well, long. Is there any way to optimise it somehow? Thanks!
Upvotes: 0
Views: 579
Reputation: 1376
Well first off, you might consider changing the UISlider so that the minimum and maximum values matched the output that you want -- i.e. min==0, max=99. Those would be the minimumValue and maximumValue properties if you want to do it in code or the Minimum and Maximum fields in the Attributes Inspector if you're using Interface Builder.
Then the computation of the output value would be:
int value = slider.value;
value = (value + 10) - value % 10;
Upvotes: 0
Reputation: 8460
once try like this i think here no need to use loop every time,
- (void)slider03Changed:(UISlider *)slider{
float value = slider.value;
int k=((int)(value*10)*10)+10;
if(k==110){
k=100;
}
lbl01.text = [NSString stringWithFormat:@"%d hours", value];
}
Upvotes: 1
Reputation: 5683
Use for loop :
- (void)slider03Changed:(UISlider *)slider{
int value = 10;
for (float i = 0.9; i > 0; i-=0.1)
{
if (slider.value >= i)
{
value = (i*100)+10;
break;
}
}
lbl01.text = [NSString stringWithFormat:@"%u hours", value];
}
The formula used is:
(i*100)+10
For example:
(0.6*100)+10 = 70
(0.7*100)+10 = 80
and so on...
UPDATE:
You can optimize it further by start checking this case slider.value >= 0.9
first.
Why ?
Because if it satisfy the case with higher value, then the rest of the your if
statement will be useless. In the code I wrote, whenever the condition with most value of slider.value
is satisfied, we set the value of value
variable and break out of the loop. This will prevent your code to evaluate unnecessary cases.
For example:
Assume your slider.value
= 0.95
This is what will happen in your code:
(slider.value >= 0.1) ----> true ----> then ---> set value = 20
(slider.value >= 0.2) ----> true ----> then ---> set value = 30
(slider.value >= 0.3) ----> true ----> then ---> set value = 40
(slider.value >= 0.4) ----> true ----> then ---> set value = 50
(slider.value >= 0.5) ----> true ----> then ---> set value = 60
(slider.value >= 0.6) ----> true ----> then ---> set value = 70
(slider.value >= 0.7) ----> true ----> then ---> set value = 80
(slider.value >= 0.8) ----> true ----> then ---> set value = 90
(slider.value >= 0.9) ----> true ----> then ---> set value = 100
With my code:
(slider.value >= 0.9) ----> true ----> then ---> set value = 100 and break out of the loop
As you may see, with my code, it evaluates only 1 case.
Upvotes: 3
Reputation: 46563
Whenever you need many if
's and you know only one is going to be executed, use if-else ladder
.
In this case you can use as:(this is not the best way)
if (slider.value >= 0.9){
value = 100;
}
else if (slider.value >= 0.8){
value = 90;
}
else ....
Now to automate/making shorter code, this you can use as :
- (void)slider03Changed:(UISlider *)slider{
lbl01.text = [NSString stringWithFormat:@"%.0f hours", round(value*10-.5)*10+10]);];
}
Upvotes: 1