Possible Duplicate:
Declaring variables inside a switch statement
I'm having difficulty getting XCode to let me write a particular switch statement in Objective-C. I'm famiiar with the syntax and could rewrite it as if/else blocks but I'm curious.
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;
}
I can't see the syntax error, it's as though I need to trick the compiler into allowing this.
You can not add variable declaration after the label. You can add a semicolon instead of call to NSLog()
for instance. Or declare variable before the switch. Or add another {}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With