Objective-C Switch语句

Ech*_*lon 10 objective-c switch-statement ios

可能重复:
在switch语句中声明变量

我很难让XCode让我在Objective-C中编写一个特定的switch语句.我很熟悉语法,可以像/ if块一样重写它,但我很好奇.

switch (textField.tag) {
        case kComment:
            ingredient.comment = textField.text;
            break;
        case kQuantity:
            NSLog(@""); // removing this line causes a compiler error           
            NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
            fmt.generatesDecimalNumbers = true;
            NSNumber *quantity = [fmt numberFromString:textField.text];
            [fmt release]; 
            ingredient.quantity = quantity;
            break;
    }
Run Code Online (Sandbox Code Playgroud)

我看不到语法错误,就好像我需要欺骗编译器允许这样.

Mic*_*ker 15

您不能在标签后添加变量声明.例如,您可以添加分号而不是调用NSLog().或者在切换之前声明变量.或者添加另一个{}.