I have a method, which gets executed, say 20 times. My method has an argument, which has 20 unique values(integer value from 1 to 20), each time when the program control enters the method. The integer value is generated in a random order. First time when entering the method, the arguement may have the value ‘n’, the second time it may have value (n-3), third time it may be (n+6) and so on. Right now I am using switch case method to handle this. That is, here I have 20 cases, I am handling the 20 cases using switch-case. Is there a better way, that I can handle this situation, that is without writing 20 switch-cases?
- (CGFloat)methodName:(NSIndexPath *)indexPath{
numberOfLines = //gets this value after some complex calculations.
switch(indexPath){
case 0:
//statement
return numberOfLines*35;
break;
case 1:
//statement
return numberOfLines*35;
break;
….
…….
20 cases
}
return 35;
}
It really depends on the type of work you are doing in the switch statements. If for example you are simply returning a value based on the input then it would be possible to construct a NSDictionary mapping integers to some value. Or better yet if the input is indeed a range of consecutive integers then a predefined array would work as well.