views:

69

answers:

2

Okay, so this is weird

I have this code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
  case 1:
    NSLog(@"Platform Cell Selected");
    AddGamePlatformSelectionViewController *platformVC =
      [[AddGamePlatformSelectionViewController alloc]
      initWithNibName:@"AddGamePlatformSelectionViewController" bundle:nil];
    platformVC.context = context;
    platformVC.game = newGame;
    [self.navigationController pushViewController:platformVC animated:YES];
    [platformVC release];
    break;
  default:
    break;
  }
}

Which works fine.

When I remove the NSLog Statement, like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
  case 1:
    //NSLog(@"Platform Cell Selected");
    AddGamePlatformSelectionViewController *platformVC =
      [[AddGamePlatformSelectionViewController alloc]
      initWithNibName:@"AddGamePlatformSelectionViewController" bundle:nil];
    platformVC.context = context;
    platformVC.game = newGame;
    [self.navigationController pushViewController:platformVC animated:YES];
    [platformVC release];
    break;
  default:
    break;
  }
}

I get the following compiler errors

/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102: error: expected expression before 'AddGamePlatformSelectionViewController'

/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103: error: 'platformVC' undeclared (first use in this function)

If I just edit out the two // for commenting out that line, everything works swimingly.

A: 

Do you get the same error if you delete the NSLog statement instead of commenting it out? Maybe the compiler just doesn't like that you're starting a case block with a comment. (Ridiculous, I know, but worth a shot?)

Dan M
+4  A: 

You can't declare an object (e.g. AddGamePlatformSelectionViewController *platformVC) as the first line in case...

You can solve it by adding some code before than (e.g. NSLog) or by enclosing the code inside the case between { ... } like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  switch (indexPath.row) {
    case 1:
    {
      AddGamePlatformSelectionViewController *platformVC = [[AddGamePlatformSelectionViewController alloc]
      initWithNibName:@"AddGamePlatformSelectionViewController" bundle:nil];
      // the rest of the code...
      break;
    }
  }
}
Michael Kessler
Interesting. Thanks! I learned something new.
DVG
Please mark this as answered.
Nic Hubbard