So I’m making a converter app, and I ran into a problem when using if statements to calculate my answer. I have a UIPickerWheel to select convertFrom and convertTo, and a textField for input.
My problem is that, using below mentioned code, the answer label just displays random units, not the ones selected in the pickerWheel.
I think that my fault is the if statements not checking for both to be the same thing, just one of them. How do I make it check for both to be true, or do it in a proper way?
Code:
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float mtpaTilMtpaFloat = convertValue * 1;
float mtpaTilMMcfdayFloat = convertValue * 2;
float mtpaTilMillSm3dayFloat = convertValue * 3;
float mtpaTilMMBTUFloat = convertValue * 4;
float mtpaTilPJPAFloat = convertValue * 5;
float MMcfdayTilmtpaFloat = convertValue * 0.5;
float MMcfdayTilMMcfdayFloat = convertValue * 1;
float MMcfdayTilMillSm3dayFloat = convertValue *6;
float MMcfdayTilMMBTUFloat = convertValue *7;
float MMcfdayTilPJPAFloat = convertValue *8;
float MillSm3dayTilMTPAFloat = convertValue /3;
NSString *mtpaTilmtpa = [[NSString alloc ] initWithFormat:
@" %f MTPA = %f MTPA", convertValue, mtpaTilMtpaFloat];
NSString *mtpaTilMMcfday = [[NSString alloc ] initWithFormat:
@" %f MTPA = %f MMcf/day", convertValue, mtpaTilMMcfdayFloat];
NSString *mtpaTilMillSm3day = [[NSString alloc] initWithFormat:
@" %f MTPA = %f Mill.SM3/day", convertValue, mtpaTilMillSm3dayFloat];
NSString *mtpaTilMMBTU = [[NSString alloc] initWithFormat:
@" %f MTPA = %f MMBTU", convertValue, mtpaTilMMBTUFloat];
NSString *mtpaTilPJPA = [[NSString alloc] initWithFormat:
@" %f MTPA = %f PJPA", convertValue, mtpaTilPJPAFloat];
NSString *MMcfdayTilmtpa = [[NSString alloc] initWithFormat:
@" %f MMcfday = %f MTPA", convertValue, MMcfdayTilmtpaFloat];
NSString *MMcfdayTilMMcfday = [[NSString alloc] initWithFormat:
@" %f MMcfday = %f MMcfday", convertValue, MMcfdayTilMMcfdayFloat];
NSString *MMcfdayTilMillSm3day = [[NSString alloc] initWithFormat:
@" %f MMcfday = %f MillSm3day", convertValue, MMcfdayTilMillSm3dayFloat];
NSString *MMcfdayTilMMBTU = [[NSString alloc] initWithFormat:
@" %f MMcfday = %f MMBTU", convertValue, MMcfdayTilMMBTUFloat];
NSString *MMcfdayTilPJPA = [[NSString alloc] initWithFormat:
@" %f MMcfday = %f PJPA", convertValue, MMcfdayTilPJPAFloat];
NSString *MillSm3dayTilMTPA = [[NSString alloc] initWithFormat:
@" %f MillSm3day = %f MTPA", convertValue, MillSm3dayTilMTPAFloat];
if (from = 1, to == 1) { resultLabel.text = mtpaTilmtpa;
}
if (from = 1, to == 2) { resultLabel.text = mtpaTilMMcfday;
}
if (from = 1, to == 3) { resultLabel.text = mtpaTilMillSm3day;
}
if (from = 1, to == 4) { resultLabel.text = mtpaTilMMBTU;
}
if (from = 1, to == 5) { resultLabel.text = mtpaTilPJPA;
}
if (from = 2, to == 1) { resultLabel.text = MMcfdayTilmtpa;
}
if (from = 2, to == 2) { resultLabel.text = MMcfdayTilMMcfday;
}
if (from = 2, to == 3) { resultLabel.text = MMcfdayTilMillSm3day;
}
if (from = 2, to == 4) { resultLabel.text = MMcfdayTilMMBTU;
}
if (from = 2, to == 5) { resultLabel.text = MMcfdayTilPJPA;
}
if (from = 3, to == 1) { resultLabel.text = MillSm3dayTilMTPA;
}
}
Thanks.
You only have a single
=in most of your statements. You should be using a double equals (==). A single=is going to assign the value tofrom, not check for equality. I would also replace the,with&&. One final thing to do is to combine the common parts of theifstatements.