1

In Swift it is possible to check a tuple like,

switch(type1, type2) {
    case (1, 1):
        functionNormal()
    case (1, 2):
        functionUncommon()
    case (1, 3):
        functionRare()
    ...
}

Is it possible to check tuple like multiple values in Objective-C Switch case? Any way around?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

1 Answers1

2

There could be various approaches, depending on exactly what your type1 and type2 data might be.

However, here is a basic example:

NSInteger i = 1;
NSInteger j = 2;

switch (i) {
    case 1:
        switch (j) {
            case 1:
                [self functionNormal];
                break;

            case 2:
                [self functionUncommon];
                break;

            case 3:
                [self functionRare];
                break;

            default:
                NSLog(@"first value: 1 ... missing case for second value: for %ld", (long)j);
                break;
        }
        break;

    case 2:
        switch (j) {
            case 1:
                [self secondFunctionNormal];
                break;

            case 2:
                [self secondFunctionUncommon];
                break;

            case 3:
                [self secondFunctionRare];
                break;

            default:
                NSLog(@"first value: 2 ... missing case for second value: %ld", (long)j);
                break;
        }
        break;

    default:
        NSLog(@"missing first case for first value: %ld", (long)i);
        break;
}

This is rather inefficient, of course, but maybe it can get you on your way.


Edit

Again, it will depend on your data, but another approach more closely resembling your Swift example:

NSInteger i = 1;
NSInteger j = 2;

NSInteger ij = i * 1000 + j;

switch (ij) {
    case 1001:
        [self functionNormal];
        break;

    case 1002:
        [self functionUncommon];
        break;

    case 1003:
        [self functionRare];
        break;

    case 2001:
        [self secondFunctionNormal];
        break;

    case 2002:
        [self secondFunctionUncommon];
        break;

    case 2003:
        [self secondFunctionRare];
        break;

    default:
        NSLog(@"case was something else: %ld", (long)ij);
        break;
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Although impractical, your second option is not theoretically 100% accurate because it might end up with a conflict for `i=0, j=1000` with `i=1, j = 0` and it worth mentioning in your solution. Thanks. – Sazzad Hissain Khan Jan 23 '20 at 10:42
  • @SazzadHissainKhan - exactly, which is why I stated ***depending on exactly what your type1 and type2 data might be.*** For that specific example, you would multiply `I` by a higher value than the max that `j` could reach. Probably worth repeating: ***There could be various approaches*** - neither of these is what *I* would do, but I believe it answers the question of *"Any way around?"* – DonMag Jan 23 '20 at 13:22