最近在写一个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;
}
}