Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing this: Cannot jump from switch statement to this case label [duplicate]

This is a switch statement that I am getting errors on:

        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

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

like image 379
SpokaneDude Avatar asked Jan 16 '16 17:01

SpokaneDude


People also ask

Can switch case have duplicates?

If two cases in a 'switch' statement are identical, the second case will never be executed. This most likely indicates a copy-paste error where the first case was copied and then not properly adjusted.

What does the error jump to case label mean?

The problem is that variables declared in one case are still visible in the subsequent case s unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case .

How do you fix a case label not within a switch statement?

How to fix? See the statement switch(choice); it is terminated by semicolon (;) – it must not be terminated. To fix this error, remove semicolon after this statement.

What is case label in switch statement?

A case or default label can only appear inside a switch statement. The constant-expression in each case label is converted to a constant value that's the same type as condition . Then, it's compared with condition for equality.

Why do I get a jump to case error when declaring variables?

Error: Jump to case label Solution 1: The problem is that variables declared in one case are still visible in the subsequent cases unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case.

What is considered a jump in a switch statement?

87) The transfer from the condition of a switch statement to a case label is considered a jump in this respect. void f () { // ... goto lx; // ill-formed: jump into scope of a // ... ly: X a = 1; // ... lx: goto ly; // OK, jump implies destructor // call for a followed by construction // again immediately following label ly }

How to solve [solved-2 solutions] error [1] jump to case label?

[Solved-2 Solutions] Error: Jump to case label 1 Error Description:. 2 Solution 1:. The problem is that variables declared in one case are still visible in the subsequent cases unless an... 3 Solution 2:. Declaration of new variables in case statements is what causing problems. Enclosing all casestatements in... More ...

Why are variables declared in one case not initialized in another case?

The problem is that variables declared in one case are still visible in the subsequent cases unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case.


1 Answers

C is not Swift. You'll be happier if you structure your switch statements using curly braces round all of the cases interiors, like this:

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.

like image 63
matt Avatar answered Sep 28 '22 00:09

matt