griotspeak
griotspeak

Reputation: 13247

Why doesn't this bit of code work?

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 if ([indexPath section] == 0) {
  switch ([indexPath row]) {

   case 0:
    [self renameExercise];
    [[self tableView] deselectRowAtIndexPath:indexPath 
            animated:YES];
    break;

   case 1:
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView"
                           bundle:nil];
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [newController setDelegate:self];
    [self presentModalViewController:newController
          animated:YES];
    [newController release];
    break;

xCode 'expects expression before EditRootNoteViewController...but why come? this same bit of code works outside this switch...which is probably some sort of clue, but i haven't the slightest of what.

Upvotes: 1

Views: 149

Answers (3)

W Dyson
W Dyson

Reputation: 4634

Is this your entire switch statement? If so, you're forgetting the

default:
break;

section.

Make sure your question includes the full method, or at least a complete one, to make it easier for us to help.

EDIT: Oh! After looking at this for a second time, if you declare new variables in a switch statement, you have to do so within curly brackets. Not sure why exactly, I ran into this problem a few weeks ago. Maybe someone can elaborate as to why this is needed?

Upvotes: 1

Dave DeLong
Dave DeLong

Reputation: 243156

It's because you can't declare a variable as the first statement of a case in a switch statement.

See this question or this question for more information.

Upvotes: 2

Alejandro
Alejandro

Reputation: 3746

Try putting the code in a block:

...
  switch ([indexPath row]) {

   case 0:
    [self renameExercise];
    [[self tableView] deselectRowAtIndexPath:indexPath 
            animated:YES];
    break;

   case 1: {
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView" bundle:nil];
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [newController setDelegate:self];
    [self presentModalViewController:newController animated:YES];
    [newController release];
    } break;
  }

Or better yet, extract that code into a separate method.

Upvotes: 2

Related Questions