0

I am using switch statements in Objective-C, and when I nest a switch statement inside another switch statement, a lot of errors are being found by the compiler. So my general question is how do switch statements affect what methods I can call, and what type of behavior is causing the compiler to generate errors?

switch ([indexPath section]) 
{
    case 0:
        cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row];
        CountdownFormatter *formatter = [[CountdownFormatter alloc] init];
        switch (indexPath.row) {
            case 0:
                cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.duration];
                break;
            case 1:
                cell.detailTextLabel.text = [formatter stringForCountdownInterval:taskInfo.elapsedTime];
            default:
                break;
        }
        [formatter release];
        break;
    case 1:
        cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row];
        //NSArray *segments = [NSArray arrayWithObjects:@"Low", @"Medium", @"High", nil];
        switch (indexPath.row) {
            case 0:
                cell.detailTextLabel.text = priority;
                [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
                break;
            case 1:
                [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
                UISwitch *switch = [[UISwitch alloc] init];
                break;
            case 2:

                break;
            default:
                break;
        }
        break;
    case 2:
        cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row];
        break;
    default:
        break;
}

Here is where I am having issues. Why can I not declare a UISwitch inside this nested switch statement? I know I wrote [[UISwitch alloc] init] but the compiler is flagging it as an error. Any reason that would happen?

gurooj
  • 2,100
  • 4
  • 21
  • 25
  • There is nothing wrong with nesting switch statements. Why don't you post your code and error message so we can try to fix it? – ughoavgfhw Jan 25 '12 at 23:17
  • I was trying not to just have people directly edit my code, I'd rather figure it out for myself, but clearly something is going on here of which I am not aware. – gurooj Jan 25 '12 at 23:30

3 Answers3

2

I can't say it any better than this answer Why can't variables be declared in a switch statement?.

The issue is where/when you are declaring variables and the scope they are in.

Community
  • 1
  • 1
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • Yea, I stumbled upon something similar. I didn't realize that was the issue. I ended up asking the wrong question, but arriving at the correct answer. – gurooj Jan 25 '12 at 23:46
1

when I nest a switch statement inside another switch statement, a lot of errors are being found by the compiler

Works fine for me:

int main()
{
    int x = 1;
    int y = 2;

    switch(x)
    {
    case 1:
       switch(y)
       {
       case 2:
         printf("Hello!\n");
       }
    }

    return 0;
}

So my general question is how do switch statements affect what methods I can call

They don't.

and what type of behavior is causing the compiler to generate errors?

The programmer's behaviour.

EDIT - In response to the code you posted, without being able to compile or see what the error is I would suspect:

case 1:
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *switch = [[UISwitch alloc] init];
        break;

could be:

case 1:
{
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UISwitch *myswitch = [[UISwitch alloc] init];
        break;
}

Note the braces and the choice of naming the variable something other than a keyword.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
1

Switch statements have no effect whatsoever on obj-c. They're pure C constructs, and work in Obj-C exactly as they do in C. As for errors, you likely have some sort of syntactic error if you're getting compiler errors when nesting switch statements.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347