2017-03-12 4 views
0

アプリの購入トランザクションが成功すると、データをデータベースに投稿しようとしています。つまり、私は以下のコードを使用すると、Xcodeは私にエラーを投げます:iOS - switch文からこのcaseラベルにジャンプできませんか?

"Cannot jump from switch statement to this case label"

どうすればこの問題を解決できますか?下記のコードを参照してください。

.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; 
     } 
    } 
} 
+1

? – Saranjith

+1

提案が1つあります。投稿したコードは読みにくい場合があります。 'case'ステートメントのコードの数が2行以上になると、そのコードのメソッドを作成し、そのメソッドを呼び出してから' break'メソッドを呼び出します。私は 'SKPaymentTransactionStatePurchased'ケースのコードをすべて自分のメソッドに入れることをお勧めします。 – rmaddy

+0

@Saranjith SKPaymentStateTransactionRestoredとSKPaymentStateTransactionFailed – Brittany

答えて

0

私は単にあなたの場合には{}を追加し、前と同じ問題を抱えていた、すべての問題が解決されます。それがポイントしている場合、ラベル

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; 
} 
関連する問題