Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

iphone - Compile Error with: switch, "expected expression before"

Cut to the chase I have recreated my problem as it is fairly self explanatory.

this complies without error:

switch (n) {
    case 1:
        NSLog(@"");
        NSString *aStr;
        break;
    default:
        break;
    }

this compiles with error and it's only missing the NSLog():

switch (n) {
    case 1:
        NSString *aStr;
        break;
    default:
        break;
    }

it throws an error at compile "Expected expression before 'NSString'"

Am I missing something here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In normal C you'd have to enclose this in brackets in both cases. I suspect this may fix your problem:

case 1:
{
    NSLog(@"");
    NSString *aStr;
    break;
}

See this SO question for more info.

Another way to get around this problem is to put a statement between the case label and the first declaration as you've done in your working example above. See the comments and Quinn Taylor's answer for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...