Take the 2-minute tour ×

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.

    case 0:
        return numberOfLines*35;       

    case 1:
        return numberOfLines*35;             
    20 cases


return 35;


share|improve this question
Your question isn't clear. Can you post part of the code that you wish to optimize? –  PengOne Jan 13 '12 at 6:01
Agreed. The generality of your question makes it really hard to answer. –  Jesse Bunch Jan 13 '12 at 6:02
To elaborate on the other comments, the possibility of doing something more efficient, or at least more readable than using a switch case statement depends entirely on the specifics of the method and what it is intended to do. –  Andrew Madsen Jan 13 '12 at 6:03
Post your code with switch.We must see statements in switch. –  shift66 Jan 13 '12 at 6:05
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer


