Can Objective-C switch on NSString?

后端 未结 14 1116
不知归路
不知归路 2020-12-04 05:40

Is there a more intelligent way to rewrite this?

if ([cardName isEqualToString:@\"Six\"]) {
    [self setValue:6];
} else if ([cardName isEqualToString:@\"Se         


        
14条回答
  •  暖寄归人
    2020-12-04 06:28

    You can use macros approach to achieve it:

    #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;
        }
     }
    

提交回复
热议问题