使用switch报错:Expected expression

2018-02-06  本文已影响32人  伯牙呀

使用switch,写如下的代码时:

int type = 0;
switch (type) {
    case 0:
        ViewController *vc = [ViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        break;
    default:
        break;
}

Xcode会提示错误:Expected expression

解决办法:在case块开始与结尾加入花括号{ },如下

int type = 0;
switch (type) {
    case 0:
    {
        ViewController *vc = [ViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
        break;
    default:
        break;
}

防遗忘

上一篇下一篇

猜你喜欢

热点阅读