【问题标题】:Using case switch instead of multiple if statements for error handling使用 case switch 而不是多个 if 语句进行错误处理
【发布时间】:2013-04-16 12:22:58
【问题描述】:

我正在构建一个通过移动 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: 这样的单独方法?

如何在上面的示例中编写此开关?

【问题讨论】:

    标签: ios objective-c if-statement switch-statement


    【解决方案1】:

    在这种情况下,您是使用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];
    

    【讨论】:

    • 那是完美的。从您的回答中,我完全理解这是如何工作的,如何添加更多,正是我所追求的。谢谢
    【解决方案2】:

    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] ...
    

    【讨论】:

    • 您能否详细说明如何将我现有的代码合并到 switch 语句中?
    • 谢谢,但我不太了解这些设置。你有一个 typedef 枚举作为 kTypeError,然后是 aTypeError 的开关。在 imp 文件中的任何地方定义的 typedef 在哪里?参数如何传递到交换机本身?就像我收到的返回的错误代码一样?开关方面的区别在于 UIAlertView 信息,所以我认为这将在开关本身内?
    • aTypeErrorkTypeError 类型的变量。 typedef enum 可以在 pch 文件中定义,也可以在您拥有的常量文件中定义。怎么过?好吧,它将基于您自己的架构(我显然不知道)。
    • 除了这个 typedef,你还可以使用你的 const 值,比如 kPFErrorWhatever。但是如果你想使用一个干净的枚举,你也可以给 typedef 赋值(我会编辑帖子来说明我的意思)
    【解决方案3】:
    if (!error) {
        // Handle error (?).
    }
    
    switch ([error code]) {
        case kPFErrorObjectNotFound:
            // Handle error.
            break;
        case kPFErrorConnectionFailed:
            // Handle error.
            break;
        default:
            // Handle error.
    }
    

    这仅在-code 返回的值可以在switch 测试表达式中使用时才有效。 AFAIK,int 受支持——我不知道其他类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多