2016-01-16 12 views
19

Questa è una dichiarazione interruttore che sto ottenendo gli errori sul:cosa sta causando questo: Non può saltare da un'istruzione switch per questo caso l'etichetta

 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; 
    } 

Gli ultimi due casi, più il valore predefinito, mi stanno dando la seguente errore:

Cannot jump from switch statement to this case label

Ho usato l'istruzione di interruttore molte, molte volte; questa è la prima volta che ho visto questo. Il codice è stato copiato da un tutorial (here), che sto cercando di adattare alla mia app. Gradirei l'aiuto su questo. SD

+1

Potresti provare a inserire gli interni di tutti i blocchi 'case' nelle parentesi graffe e vedere se questo cambia qualcosa? In questo modo: http://www.apeth.com/iOSBook/ch01.html#EXswitch – matt

+1

Non è possibile dichiarare "NSError * error = nil;" all'interno del caso senza racchiuderlo in un blocco (parentesi graffe) – Wain

risposta

55

C non è Swift. Sarai più felice se si struttura il switch dichiarazioni utilizzando parentesi graffe turno tutti i casi interni, come this:

switch (tag) { 
    case 1: { // curly braces 
     // ... 
     break; 
    } 
    case 2: { // curly braces 
     // ... 
     break; 
    } 
    case 3: { // curly braces 
     // ... 
     break; 
    } 
} 

L'ulteriore livello di parentesi graffe ti permette di fare cose che non puoi fare altrimenti.

+0

Incredibile! Non sono riuscito a vedere gli "alberi nella foresta". Lo sapevo, non riuscivo a pensarci ... grazie Matt SD – SpokaneDude

+0

Uno dei tanti motivi per cui non uso mai 'switch' in C ... :) Troppe trappole per i miei gusti. – matt