Can Objective-C switch on NSString?

后端 未结 14 1106
不知归路
不知归路 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条回答
  •  -上瘾入骨i
    2020-12-04 06:05

    I'm kind of late to the party, but to answer the question as stated, there's a more intelligent way:

    NSInteger index = [@[@"Six", @"Seven", @"Eight", @"Nine"] indexOfObject:cardName];
    if (index != NSNotFound) [self setValue: index + 6];
    

    Note that indexOfObject will look for the match using isEqual:, exactly as in the question.

提交回复
热议问题