美文网首页
使用switch报错:Expected expression

使用switch报错:Expected expression

作者: 伯牙呀 | 来源:发表于2018-02-06 16:46 被阅读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;
}

防遗忘

相关文章

网友评论

      本文标题:使用switch报错:Expected expression

      本文链接:https://www.haomeiwen.com/subject/zlwgzxtx.html