性質,我發現一些代碼,看起來像:遍歷在Objective-C
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];
}
這樣重複很多次。顯然,它不會很乾,並且有點痛苦。我如何循環這個邏輯與變量屬性名稱? Objective-C不允許我採取的方法。下面是我的嘗試:
NSArray * properties = [[NSArray alloc] initWithObjects:
@"foo",
@"bar",
@"spam",
nil];
for (id prop in properties) {
NSLog(@"%@",obj.prop);
}
- 注意 -
我原來的僞代碼是相當混亂。對於那個很抱歉。
簡而言之,我怎樣才能重構我的代碼,以免我不斷重複自己?所執行的數學運算總是相同的。
啊,對不起,這是一個有點混亂。如果對象是'obj',我想遍歷它的屬性,有效地做'NSLog(@「%@」,obj.foo);',然後'NSLog(@「%@」,obj.bar); '等 –