I found some code that looks like:
if (statisticsObject.idag3_orig != 0) {
statisticsView.idag3.text = [NSString stringWithFormat:@"%i",statisticsObject.idag3_orig];
} else {
float compare1 = statisticsObject.idag2;
float compare2 = statisticsObject.idag3;
float result = compare1 + (compare1 * (compare2 / (float) 100.00));
int final = (int)roundf(result);
statisticsView.idag3.text = [NSString stringWithFormat:@"%i",final];
}
if (statisticsObject.igar3_orig != 0) {
statisticsView.igar3.text = [NSString stringWithFormat:@"%i",statisticsObject.igar3_orig];
} else {
float compare1 = statisticsObject.igar2;
float compare2 = statisticsObject.igar3;
float result = compare1 + (compare1 * (compare2 / (float) 100.00));
int final = (int)roundf(result);
statisticsView.igar3.text = [NSString stringWithFormat:@"%i",final];
}
This is repeated many, many times. Obviously it doesn't feel very DRY, and is a bit of a pain to work with. How can I loop this logic with variable property names? I think the approach I've taken isn't allowed by Objective-C. Here's what I tried:
NSArray * properties = [[NSArray alloc] initWithObjects:
@"foo",
@"bar",
@"spam",
nil];
for (id prop in properties) {
NSLog(@"%@",obj.prop);
}
-- note --
My original pseudo-code was rather confusing. Sorry about that.
To put it simply, how can I restructure my code above so that I'm not constantly repeating myself? The mathematical operations performed are always the same.