175

Is there a more intelligent way to rewrite this?

if ([cardName isEqualToString:@"Six"]) {
    [self setValue:6];
} else if ([cardName isEqualToString:@"Seven"]) {
    [self setValue:7];
} else if ([cardName isEqualToString:@"Eight"]) {
    [self setValue:8];
} else if ([cardName isEqualToString:@"Nine"]) {
    [self setValue:9];
} 
6
  • 1
    No, switch only works on int/bool/char/etc types.
    – chown
    Commented Nov 17, 2011 at 3:29
  • 3
    There are several alternative ways to do it. Eg, load an array with the values and search for a match in the array. None terribly efficient, but they do reduce code duplication.
    – Hot Licks
    Commented Nov 17, 2011 at 3:30
  • 4
    As a side note, Apple's new language (Swift) now allows for comparing strings in a switch statement!
    – jaredsmith
    Commented Jun 2, 2014 at 21:53
  • 4
    Switch to Swift ;)
    – tothemario
    Commented Mar 30, 2016 at 22:35
  • 1
    Swift is not a better language. It's not worth it. Commented Jan 11, 2018 at 21:57

14 Answers 14

154

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.

3
  • 4
    @abbood For more information about enum, see the posting NS_ENUM & NS_OPTIONS by Mattt Thompson. Commented Oct 21, 2013 at 21:32
  • @abbood what is your comment supposed to mean ? Sounds like this is a bad answer but seems Ok to me. Could you explain ? Commented May 12, 2015 at 22:30
  • As I understand, CardType cannot be equal to any enclosed @"" eg: [CardType isEqualToString:@"Three"]
    – Adro
    Commented Jul 18, 2017 at 7:53
125

You could set up a dictionary of blocks, like this:

NSString *lookup = @"Hearts"; // The value you want to switch on

typedef void (^CaseBlock)();

// Squint and this looks like a proper switch!
NSDictionary *d = @{
    @"Diamonds": 
    ^{ 
        NSLog(@"Riches!"); 
    },
    @"Hearts":
    ^{ 
        self.hearts++;
        NSLog(@"Hearts!"); 
    },
    @"Clubs":
    ^{ 
        NSLog(@"Late night coding > late night dancing"); 
    },
    @"Spades":
    ^{ 
        NSLog(@"I'm digging it"); 
    }
};

((CaseBlock)d[lookup])(); // invoke the correct block of code

To have a 'default' section, replace the last line with:

CaseBlock c = d[lookup];
if (c) c(); else { NSLog(@"Joker"); }

Hopefully Apple will teach 'switch' a few new tricks.

5
  • 37
    I can't tell if this is really nasty or really cool. Would have never thought of doing this, thanks.
    – endy
    Commented May 28, 2013 at 21:11
  • 2
    While we're doing weird stuff like this, why not make your own class which wraps an NSDictionary full of NSString keys for block objects and then provides another block for default cases? You can even have it support the subscript notation. Commented Jun 16, 2013 at 15:02
  • 1
    Extra points if you make an NSDictionary subclass just for this :P
    – CommaToast
    Commented Jan 18, 2016 at 20:11
  • 2
    Under the hood, this is the way C# does it for large switch statements. Commented Mar 21, 2017 at 13:28
  • This is fantasy
    – L33MUR
    Commented Mar 6, 2021 at 16:09
86

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;
}
11
  • 1
    I like this. It answers the needs of most looking for an answer to this problem, it doesn't take much more typing than a similar switch would take in javascript, and it's human readable. Commented Mar 4, 2014 at 0:53
  • 4
    I would not compare this hack with JS switch. What happens if the next programmer add an item between item1 and item2? Too much potential for introducing bugs
    – Aras
    Commented Mar 20, 2014 at 1:25
  • 1
    its a nice hack though, so I give you thumbs up for the effort :)
    – Aras
    Commented Mar 20, 2014 at 1:26
  • @Aras If the next programmer needs to add a new entry then they would add it to the end of the array with a new case statement at the end to handle it. So @"item0" can be added after @"item3" in the array, then add a case 3: to handle it.
    – sbonkosky
    Commented Mar 21, 2014 at 15:56
  • 1
    I totally Like your way. It very neat. I am writing category and need to return UIColor while I have string with me.
    – Alok C
    Commented Apr 5, 2014 at 12:57
11

Unfortunately, switch statements can only be used on primitive types. You do have a few options using collections, though.

Probably the best option would be to store each value as an entry in an NSDictionary.

NSDictionary *stringToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [NSNumber numberWithInt:6],@"Six",
                                              [NSNumber numberWithInt:7],@"Seven",
                                              [NSNumber numberWithInt:8],@"Eight",
                                              [NSNumber numberWithInt:9],@"Nine",
                                              nil];
NSNumber *number = [stringToNumber objectForKey:cardName];
if(number) [self setValue:[number intValue]];
9

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
1
  • This is interesting. Can you elaborate more? Commented Dec 22, 2016 at 10:00
6

Here is the more intelligent way to write that. It's to use an NSNumberFormatter in the "spell-out style":

NSString *cardName = ...;

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterSpellOutStyle];
NSNumber *n = [nf numberFromString:[cardName lowercaseString]];
[self setValue:[n intValue]];
[nf release];

Note that the number formatter wants the string to be lowercased, so we have to do that ourselves before passing it in to the formatter.

5

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]];
}
4

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
3

Objective-c is no different from c in this aspect, it can only switch on what c can (and the preproc def's like NSInteger, NSUInteger, since they ultimately are just typedef'd to an integral type).

Wikipedia:

c syntax:

The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type.

Integral Types:

In computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values.

2

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.

2

Building on @Graham Perks idea posted earlier, designed a simple class to make switching on strings fairly simple and clean.

@interface Switcher : NSObject

+ (void)switchOnString:(NSString *)tString
                 using:(NSDictionary<NSString *, CaseBlock> *)tCases
           withDefault:(CaseBlock)tDefaultBlock;

@end

@implementation Switcher

+ (void)switchOnString:(NSString *)tString
                 using:(NSDictionary<NSString *, CaseBlock> *)tCases
           withDefault:(CaseBlock)tDefaultBlock
{
    CaseBlock blockToExecute = tCases[tString];
    if (blockToExecute) {
        blockToExecute();
    } else {
        tDefaultBlock();
    }
}

@end

You would use it like this:

[Switcher switchOnString:someString
                   using:@{
                               @"Spades":
                               ^{
                                   NSLog(@"Spades block");
                               },
                               @"Hearts":
                               ^{
                                   NSLog(@"Hearts block");
                               },
                               @"Clubs":
                               ^{
                                   NSLog(@"Clubs block");
                               },
                               @"Diamonds":
                               ^{
                                   NSLog(@"Diamonds block");
                               }
                           } withDefault:
                               ^{
                                   NSLog(@"Default block");
                               }
 ];

The correct block will execute according to the string.

Gist for this solution

2

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 (@"TestString") {
        break;
    }
    CASE (@"YetAnotherString") {
        break;
    }
    CASE (@"Test") {
        break;
    }
    DEFAULT {
        break;
    }
 }
0

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"

-1
typedef enum
{
    Six,
    Seven,
    Eight
} cardName;

- (void) switchcardName:(NSString *) param {
    switch([[cases objectForKey:param] intValue]) {
        case Six:
            NSLog(@"Six");
            break;
        case Seven:
            NSLog(@"Seven");
            break;
        case Eight:
            NSLog(@"Eight");
            break;
        default: 
            NSLog(@"Default");
            break;
    }
}

Enjoy Coding.....

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.