为什么我不能在开关块中创建变量?

时间:2010-06-14 20:20:20

标签: objective-c

奇怪:

switch(type) {
    case NSFetchedResultsChangeInsert:
        int x = 5; // error: "Expected expression before int"

        break;
}

因此无法在switch-case-block中创建局部变量?

1 个答案:

答案 0 :(得分:12)

您尝试添加花括号吗?

switch(type) {
    case NSFetchedResultsChangeInsert:
        {
            int x = 5; // error: "Expected expression before int"

            break; 
        }
}