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

Objective CSwitch StatementIos9

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpokaneDudeView Question on Stackoverflow
Solution 1 - Objective CmattView Answer on Stackoverflow