Search code examples
iphoneuitableviewnslog

Removing NSLog breaks compiler


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.


Solution

  • 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;
        }
      }
    }