2013-04-16 20 views
3

我正在構建一個通過移動SAAS - Parse登錄的應用程序。使用大小寫切換而不是多個if語句來處理錯誤

有多個錯誤代碼可以從登錄請求返回。目前運行的,如果每個錯誤代碼語句,並顯示這樣的相關的警報視圖:

 if (error == nil) { 
      // Something went wrong 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LoginAlertErrorTitle", @"Login Error Alert View Title") message:NSLocalizedString(@"LoginStandardError", @"Login error message text - standard error") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"GlobalOKButtonTitle", @"Global Ok button title"), nil]; 
      [alertView show]; 
     } else if ([error code] == kPFErrorObjectNotFound) { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LoginAlertErrorTitle", @"Login Error Alert View Title") message:NSLocalizedString(@"LoginErrorObjectNotFound", @"Login error message text - object not found") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"GlobalOKButtonTitle", @"Global Ok button title"), nil]; 
      [alertView show]; 
     } else if ([error code] == kPFErrorConnectionFailed) { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LoginAlertErrorTitle", @"Login Error Alert View Title") message:NSLocalizedString(@"LoginAlertErrorConnection", @"Login error message text - connection failed") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"GlobalOKButtonTitle", @"Global Ok button title"), nil]; 
      [alertView show]; 
     } else { 
      NSLog(@"A Login error occurred: %i",[error code]); 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LoginAlertErrorTitle", @"Login Error Alert View Title") message:[[error userInfo] objectForKey:@"error"] delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"GlobalOKButtonTitle", @"Global Ok button title"), nil]; 
      [alertView show]; 
     } 

有沒有更有效的方式做到以案例/開關一樣嗎?

實際的錯誤代碼是設置這樣的:

/*! @abstract 100: The connection to the Parse servers failed. */ 
extern NSInteger const kPFErrorConnectionFailed; 

這讓我覺得我可以設置這樣一個case語句。這是否是正確的/最好的方法來解決這個問題?是否應該像handleErrorAlert:這樣的單獨方法可能?

我如何在上面的例子中編碼這個開關?

回答

10

無論您使用switch聲明或一系列if - else if在這種情況下真的只是一個味道問題。是的,switch聲明稍微有效一些,但是在這種情況下,它並不重要(它不像每秒呼叫數千次)。使用你發現更具可讀性的東西。

雖然您可能想重構一下警報視圖代碼,但您在所有情況下都會做同樣的事情,只有錯誤信息不同,所以會有相當多的重複代碼。你可以像這樣重構它:

NSString *errorMessage = nil; 
if (error == nil) { 
    errorMessage = NSLocalizedString(@"LoginStandardError", @"Login error message text - standard error"); 
} else { 
    switch ([error code]) { 
      case kPFErrorObjectNotFound: 
       errorMessage = NSLocalizedString(@"LoginErrorObjectNotFound", @"Login error message text - object not found"); 
       break; 
      case kPFErrorConnectionFailed: 
       errorMessage = NSLocalizedString(@"LoginAlertErrorConnection", @"Login error message text - connection failed"); 
       break; 
      default: 
       errorMessage = [[error userInfo] objectForKey:@"error"]; 
    } 
} 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LoginAlertErrorTitle", @"Login Error Alert View Title") 
                message:errorMessage 
                delegate:self 
              cancelButtonTitle:nil 
              otherButtonTitles:NSLocalizedString(@"GlobalOKButtonTitle", @"Global Ok button title"), nil]; 
[alertView show]; 
+0

這是完美的。從你的答案我完全理解這是如何工作的,如何添加更多,正是我所追求的。謝謝 – StuartM

4

A typedef enum用於switch,我認爲這將是最乾淨的方式。事情是這樣的:

typedef enum 
{ 
kServerError, 
kInternetError, 
kUnknowError 
} kTypeError; 

switch (aTypeError) 
{ 
. 
. 
. 
} 

在特定情況下,你照顧有關switch內部消息...的UIAlertView共同部分。所以:

NSString *aTitle = nil; 
NSString *aMessage = nil; 

switch (aTypeError) 
{ 
    case kUnknowError: 
    { 
     aTitle = ...; 
     aMessage = ...; 
    } 
    break; 
} 

UIAlertView *alertView = [UIAlertView alloc] ... 
+0

請你詳細說明如何將現有代碼合併到switch語句中嗎? – StuartM

+0

檢查我的編輯斯圖爾特。 – Peres

+0

謝謝,但我不明白這些的設置。你有一個typedef枚舉爲kTypeError,然後是aTypeError的開關。 imp文件中的任何位置定義的typedef在哪裏?參數如何傳遞到交換機本身?就像我收到的返回的錯誤代碼一樣?在開關方面的差異將是UIAlertView信息,所以我認爲這將在交換機本身? – StuartM

1
if (!error) { 
    // Handle error (?). 
} 

switch ([error code]) { 
    case kPFErrorObjectNotFound: 
     // Handle error. 
     break; 
    case kPFErrorConnectionFailed: 
     // Handle error. 
     break; 
    default: 
     // Handle error. 
} 

如果-code返回的值可以在switch測試表達式中使用這隻作品。 AFAIK,int被支持 - 我不知道其他類型。