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;

これにより、ケースステートメントでこれをセットアップできると思います。これはこれにアプローチするための正しい/最良の方法でしょうか? handleErrorAlert:おそらく別の方法である必要がありますか?

上記の例でこのスイッチをどのようにコーディングしますか?

4

3 に答える 3

10

この場合、ステートメントを使用するかswitch一連のif-を使用するかelse ifは、好みの問題です。はい、switchステートメントの方がわずかに効率的ですが、このような場合は問題になりません (1 秒間に何千回も呼び出す必要はありません)。より読みやすいものを使用してください。

ただし、アラート ビューのコードを少しリファクタリングすることをお勧めします。エラー メッセージが異なるだけで、すべてのケースで同じことを行っているため、かなりの量のコードが繰り返されます。次のようにリファクタリングできます。

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];
于 2013-04-16T13:01:35.770 に答える
4

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] ...
于 2013-04-16T12:23:52.363 に答える
1
if (!error) {
    // Handle error (?).
}

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

これは、によって返される値がテスト式-codeで使用できる場合にのみ機能します。switch私の知る限り、intサポートされています—他のタイプについては知りません。

于 2013-04-16T12:47:54.830 に答える