开发者

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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜