Can Objective-C switch on NSString?

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

    0 讨论(0)
  • 2020-12-04 06:06

    A bit late but for anyone in the future I was able to get this to work for me

    #define CASE(str) if ([__s__ isEqualToString:(str)])
    #define SWITCH(s) for (NSString *__s__ = (s); ; )
    #define DEFAULT
    
    0 讨论(0)
  • 2020-12-04 06:09

    There are other ways to do that, but switch isn't one of them.

    If you only have a few strings, as in your example, the code you have is fine. If you have many cases, you could store the strings as keys in a dictionary and look up the corresponding value:

    NSDictionary *cases = @{@"Six" : @6,
                            @"Seven" : @7,
                            //...
                           };
    
    NSNumber *value = [cases objectForKey:cardName];
    if (value != nil) {
        [self setValue:[value intValue]];
    }
    
    0 讨论(0)
  • 2020-12-04 06:09

    BY FAR.. my FAVORITE "ObjC Add-On" is ObjectMatcher

    objswitch(someObject)
        objcase(@"one") { // Nesting works.
            objswitch(@"b")
                objcase(@"a") printf("one/a");
                objcase(@"b") printf("one/b");
                endswitch // Any code can go here, including break/continue/return.
        }
        objcase(@"two") printf("It's TWO.");  // Can omit braces.
        objcase(@"three",     // Can have multiple values in one case.
            nil,              // nil can be a "case" value.
            [self self],      // "Case" values don't have to be constants.
            @"tres", @"trois") { printf("It's a THREE."); }
        defaultcase printf("None of the above."); // Optional default must be at end.
    endswitch
    

    AND it works with non-strings, TOO... in loops, even!

    for (id ifNumericWhatIsIt in @[@99, @0, @"shnitzel"])
        objswitch(ifNumericWhatIsIt)
            objkind(NSNumber)  printf("It's a NUMBER.... "); 
            objswitch([ifNumericWhatIsIt stringValue])
                objcase(@"3")   printf("It's THREE.\n"); 
                objcase(@"99")  printf("It's NINETY-NINE.\n"); 
                defaultcase     printf("some other Number.\n");
           endswitch
        defaultcase printf("It's something else entirely.\n");
    endswitch
    
    It's a NUMBER.... It's NINETY-NINE.
    It's a NUMBER.... some other Number.
    It's something else entirely.
    

    Best of all, there are SO few {...}'s, :'s, and ()'s

    0 讨论(0)
  • 2020-12-04 06:12

    I can't Comment on cris's answer on @Cris answer but i would like to say that:

    There is an LIMITATION for @cris's method:

    typedef enum will not take alphanumeric values

    typedef enum
    {
      12Ace, 23Two, 23Three, 23Four, F22ive ... Jack, Queen, King
    
    } CardType;
    

    So here is another One:

    Link Stack over flow Go to this user answer "user1717750"

    0 讨论(0)
  • 2020-12-04 06:13

    For me, a nice easy way:

    NSString *theString = @"item3";   // The one we want to switch on
    NSArray *items = @[@"item1", @"item2", @"item3"];
    int item = [items indexOfObject:theString];
    switch (item) {
        case 0:
           // Item 1
           break;
        case 1:
           // Item 2
           break;
        case 2:
           // Item 3
           break;
        default:
           break;
    }
    
    0 讨论(0)
提交回复
热议问题