Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Switch Statement [duplicate]

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.

like image 261
Echilon Avatar asked Dec 22 '11 14:12

Echilon


1 Answers

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 {}.

like image 164
Michael Krelin - hacker Avatar answered Nov 15 '22 20:11

Michael Krelin - hacker