Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Cannot jump from switch statement to this case label?

I'm trying to post data to my database upon a successful in app purchase transaction. That said, when I use the below code, XCode throws me the error:

"Cannot jump from switch statement to this case label"

How can I fix this? See below code.

.m

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    for(SKPaymentTransaction *transaction in transactions){
        switch(transaction.transactionState){
            case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");

                break;
            case SKPaymentTransactionStatePurchased:


                self.indicatorThree.hidden = YES;
                [self.indicatorThree stopAnimating];

                self.indicatorTwo.hidden = YES;
                [self.indicatorTwo stopAnimating];

                self.indicatorThree.hidden = YES;
                [self.indicatorThree stopAnimating];


                NSMutableDictionary *viewParams3 = [NSMutableDictionary new];
                [viewParams3 setValue:@"current_user" forKey:@"view_name"];
                [DIOSView viewGet:viewParams3 success:^(AFHTTPRequestOperation *operation, id responseObject) {

                    self.currentUser = [responseObject mutableCopy];

                    NSString *pointBalance = self.currentUser[0][@"points balance"];



                    NSMutableDictionary *userData = [NSMutableDictionary new];


                    int result = [pointBalance intValue] + [self.pointsPurchase intValue];
                    NSLog(@"THIS IS THE RESULT POINTS NUMBER %d", result);

                    NSString *resultPoints = [NSString stringWithFormat:@"%d", result];


                    NSDictionary *targetPoints = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: resultPoints, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
                    NSDictionary *finalPoints = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:targetPoints] forKey:@"und"];

                    [userData setObject:finalPoints forKey:@"field_points_balance"];

                    NSLog(@"FINAL POINTS TO POST %@", finalPoints);

                    NSDictionary *user = [[DIOSSession sharedSession] user];
                    NSString *uid = user[@"uid"];


                    [userData setObject:uid forKey:@"uid"];

                    [DIOSUser
                     userUpdate:userData
                     success:^(AFHTTPRequestOperation *op, id response) { /* Handle successful operation here */

                         NSString *userPoints = user[@"field_points_balance"][@"und"][0][@"value"];
                         NSLog(@"AFTER SAVE USER POINTS %@", userPoints);



                         [[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:nil];


                     }
                     failure:^(AFHTTPRequestOperation *op, NSError *err) { /* Handle operation failire here */

                         NSLog(@"User data failed to update!");
                     }
                     ];



                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    NSLog(@"Failure: %@", [error localizedDescription]);
                }];


                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"Transaction state -> Purchased");
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"Transaction state -> Restored");

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:

                if(transaction.error.code == SKErrorPaymentCancelled){
                    NSLog(@"Transaction state -> Cancelled");
                    //the user cancelled the payment ;(
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
        }
    }
}
like image 591
Brittany Avatar asked Dec 07 '22 19:12

Brittany


1 Answers

I had same problem before, simply add a {} in your case, all your problem will be solved.

The block definition creates a new scope which seems to interfere with the compiler's ability to correctly interpret the switch statement.

Adding scope delimiters for each case label resolves the error. I think this is because the block's scope is now unambiguously a child of the case scope.

switch (transaction.transactionState) {
    case 0:
    {
        // ...
        break;
    }
    case 1:
    {
        // ...
        break;
    }
    default:
        break;
}
like image 197
Sid Mhatre Avatar answered Dec 15 '22 01:12

Sid Mhatre