您的位置:首页 > 移动开发 > IOS开发

ios switch中case中声明变量报错“expected expression”

2015-11-24 16:33 537 查看
      最近在写一个switch语句的时候,在case块中声明了一个变量,xcode报错“expected expression”,找了很久也没找到语法错误,后来才发现Object-C在case块中声明变量需要用大括号包围,如果没有变量声明就不需要了。

    原报错语句:

switch (button.tag) {
case 41:
FaceToFaceReceiptViewController *ftfRVC = [[FaceToFaceReceiptViewController alloc] init];
[self.navigationController pushViewController:ftfRVC animated:YES];
[ftfRVC release];
break;
  


加上大括号后就正常了,无变量声明无需大括号:

int i,j,jk;
switch (button.tag) {
case 41:{
FaceToFaceReceiptViewController *ftfRVC = [[FaceToFaceReceiptViewController alloc] init];
[self.navigationController pushViewController:ftfRVC animated:YES];
[ftfRVC release];
break;
}
case 42:{
FaceToFacePaymentViewController *ftfPVC = [[FaceToFacePaymentViewController alloc] init];
[self.navigationController pushViewController:ftfPVC animated:YES];
[ftfPVC release];
break;
}
case 3:
i = 4;
j = 3;
jk = 0;
break;

default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息