2

好吧,这很奇怪

我有这个代码

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

哪个工作正常。

当我删除 NSLog 语句时,如下所示:

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

我收到以下编译器错误

/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102:错误:“AddGamePlatformSelectionViewController”之前的预期表达式

/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103: 错误: 'platformVC' undeclared (第一次使用在这个功能)

如果我只是编辑出两个 // 来注释掉那一行,那么一切都会顺利进行。

4

2 回答 2

5

您不能将对象(例如 )声明为...AddGamePlatformSelectionViewController *platformVC中的第一行case

您可以通过在比之前添加一些代码(例如NSLog)或将代码包含case在 { ... } 之间来解决它,如下所示:

- (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;
    }
  }
}
于 2010-06-10T05:21:46.290 回答
0

如果你删除NSLog 语句而不是注释掉它,你会得到同样的错误吗?也许编译器只是不喜欢你开始一个带有注释的案例块。(荒谬,我知道,但值得一试吗?)

于 2010-06-10T05:18:49.243 回答