我正在構建一個通過移動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:
這樣的單獨方法可能?
我如何在上面的例子中編碼這個開關?
這是完美的。從你的答案我完全理解這是如何工作的,如何添加更多,正是我所追求的。謝謝 – StuartM