objective-c 是什么原因造成的:无法从 switch 语句跳转到这个 case 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34829955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is causing this: Cannot jump from switch statement to this case label
提问by SpokaneDude
This is a switch statement that I am getting errors on:
这是我遇到错误的 switch 语句:
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// show wait view here
statusLabel.text = @"Processing...";
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock iClooud Syncing
statusLabel.text = @"Done!";
NSError *error = nil;
[SFHFKeychainUtils storeUsername:@"IAPNoob01" andPassword:@"whatever" forServiceName: kStoredData updateExisting:YES error:&error];
// apply purchase action - hide lock overlay and
[oStockLock setBackgroundImage:nil forState:UIControlStateNormal];
// do other thing to enable the features
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = @"";
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = @"Purchase Error!";
break;
default:
break;
}
The last two cases, plus the default, are giving me the following error:
最后两种情况,加上默认情况,给了我以下错误:
Cannot jump from switch statement to this case label
无法从 switch 语句跳转到这个 case 标签
I have used the switch statement many, many times; this is the first time I have seen this. The code has been copied from a tutorial (here), which I am trying to adapt for my app. Would appreciate the help on this one. SD
我已经多次使用 switch 语句;这是我第一次看到这个。代码是从教程(这里)中复制的,我正在尝试适应我的应用程序。将不胜感激这方面的帮助。标清
回答by matt
C is not Swift. You'll be happier if you structure your switchstatements using curly braces round all of the cases interiors, like this:
C 不是 Swift。如果您switch使用花括号围绕所有案例内部来构建您的语句,您会更快乐,如下所示:
switch (tag) {
case 1: { // curly braces
// ...
break;
}
case 2: { // curly braces
// ...
break;
}
case 3: { // curly braces
// ...
break;
}
}
The extra level of curly braces allows you to do things you can't do otherwise.
额外级别的大括号允许您做其他情况下无法做的事情。

