Using an NSString in a switch statement

Thomas Clayson picture Thomas Clayson · Nov 19, 2010 · Viewed 54k times · Source

Is it possible to use an NSString in a switch statement?

Or is it better to just use if / else if?

Answer

user1717750 picture user1717750 · Oct 3, 2012

I use these macros in my app.

#define CASE(str)                       if ([__s__ isEqualToString:(str)]) 
#define SWITCH(s)                       for (NSString *__s__ = (s); ; )
#define DEFAULT   

SWITCH (string) {
    CASE (@"AAA") {
        break;
    }
    CASE (@"BBB") {
        break;
    }
    CASE (@"CCC") {
        break;
    }
    DEFAULT {
        break;
    }
 }