使用大小写切换而不是多个if语句来进行错误处理

Stu*_*rtM 3 if-statement objective-c switch-statement ios

我正在构建一个通过移动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];
        }
Run Code Online (Sandbox Code Playgroud)

是否有更有效的方法来处理案例/切换?

实际的错误代码设置如下:

/*! @abstract 100: The connection to the Parse servers failed. */
extern NSInteger const kPFErrorConnectionFailed;
Run Code Online (Sandbox Code Playgroud)

这让我觉得我可以在案例陈述中设置这个.这是解决这个问题的正确/最佳方法吗?应该是handleErrorAlert:可能的单独方法吗?

我将如何在上面的示例中对此开关进行编码?

omz*_*omz 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];
Run Code Online (Sandbox Code Playgroud)