Can Objective-C switch on NSString?

后端 未结 14 1108
不知归路
不知归路 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:22

    Unfortunately they cannot. This is one of the best and most sought after utilizations of switch statements, so hopefully they hop on the (now) Java (and others) bandwagon!

    If you are doing card names, perhaps assign each card object an integer value and switch on that. Or perhaps an enum, which is considered as a number and can therefore be switched upon.

    e.g.

    typedef enum{
      Ace, Two, Three, Four, Five ... Jack, Queen, King
    
    } CardType;
    

    Done this way, Ace would be be equal to case 0, Two as case 1, etc.

提交回复
热议问题