可能性の重複:
Declaring variables inside a switch statementObjective-Cのswitch文
私は私はObjective-Cの中の特定のswitch文を書いてみましょうするためにはXCodeを得る難しさを抱えています。私は文法に慣れていて、if/elseブロックとして書き直すことができますが、私は興味があります。
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;
}
私は構文エラーが表示されませんが、これを許可するにはコンパイラを騙す必要があります。
...これを試してみてください。 – Ariel
はい、キーストロークの回数は最小ですが、文脈によっては最も美しい解決策が異なるかもしれません:) –
これが私があなたの答えを投票した理由です: – Ariel