簡體   English   中英

使用大小寫切換而不是多個if語句來進行錯誤處理

[英]Using case switch instead of multiple if statements for error handling

我正在構建一個通過移動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;

這讓我覺得我可以在案例陳述中設置這個。 這是解決這個問題的正確/最佳方法嗎? 它應該是一個像handleErrorAlert:這樣的單獨方法嗎?可能嗎?

我將如何在上面的示例中對此開關進行編碼?

無論你使用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];

switch上使用的typedef enum ,我認為這將是最干凈的方式。 像這樣的東西:

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] ...
if (!error) {
    // Handle error (?).
}

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

僅當-code返回的值可用於switch測試表達式時,此方法才有效。 支持AFAIK, int - 我不知道其他類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM