开关坏了,语法错误?
switch gone bad, syntax error?
出于某种原因,此 switch 语句不想识别我的视图控制器 类 名称或附加变量(IndexViewController 附加 indexVC)。如果我在常规的 if 或没有依赖语句中执行此操作,它确实会识别同一段代码。我实际上查阅了 switch 的语法,因为我认为我的思想泄漏了一些内存。但语法似乎没问题。有人可以指出我的错误吗?谢谢
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 0:
IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
[self.currentVC presentViewController:indexVC animated:YES completion:nil];
break;
case 1:
ProtectionViewController *proVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProViewController"];
[self.currentVC presentViewController:proVC animated:YES completion:nil];
break;
case 2:
UsersViewController *usersVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UsersViewController"];
[self.currentVC presentViewController:usersVC animated:YES completion:nil];
break;
case 3:
StatisticsViewController *statisticsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
[self.currentVC presentViewController:statisticsVC animated:YES completion:nil];
break;
default:
NSLog(@"We dont beleive in defaults");
break;
}
}
您需要赋予 case-local 变量作用域:
switch (indexPath.row) {
case 0: {
IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
[self.currentVC presentViewController:indexVC animated:YES completion:nil];
break;
}
case 1: {
...
}
...
添加大括号。
出于某种原因,此 switch 语句不想识别我的视图控制器 类 名称或附加变量(IndexViewController 附加 indexVC)。如果我在常规的 if 或没有依赖语句中执行此操作,它确实会识别同一段代码。我实际上查阅了 switch 的语法,因为我认为我的思想泄漏了一些内存。但语法似乎没问题。有人可以指出我的错误吗?谢谢
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 0:
IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
[self.currentVC presentViewController:indexVC animated:YES completion:nil];
break;
case 1:
ProtectionViewController *proVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProViewController"];
[self.currentVC presentViewController:proVC animated:YES completion:nil];
break;
case 2:
UsersViewController *usersVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UsersViewController"];
[self.currentVC presentViewController:usersVC animated:YES completion:nil];
break;
case 3:
StatisticsViewController *statisticsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
[self.currentVC presentViewController:statisticsVC animated:YES completion:nil];
break;
default:
NSLog(@"We dont beleive in defaults");
break;
}
}
您需要赋予 case-local 变量作用域:
switch (indexPath.row) {
case 0: {
IndexViewController *indexVC = [self.storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
[self.currentVC presentViewController:indexVC animated:YES completion:nil];
break;
}
case 1: {
...
}
...
添加大括号。