2011-05-11 19 views
12

我無法找到在iOS上一起使用NSErrorUIAlertViewNSErrorRecoveryAttempting的正確方法示例。我可以找到的大多數文檔和示例都涵蓋了OS X上的相關功能,其中相關行爲由Cocoa集成。但在iOS中,似乎有必要「親手」做這件事,而且我無法找到它如何完成的好例子。iOS中NSErrorRecoveryAttempting,NSError和UIAlertView的正確用法是什麼?

我非常感謝在使用NSError中的信息支持向用戶報告的NSErrors恢復嘗試中的最佳實踐的幾個示例。

+0

在某一時刻,我寫了一段代碼來完成這個工作(使用UIAlertView等提供NSError的句柄),但是我還沒有看到實際上有一個'recoveryAttempter'的錯誤,所以我不知道它是否正確。 – Anomie

回答

6

根據蘋果的文檔:

重要:NSError類可在Mac OS X和iOS。但是,錯誤響應程序和錯誤恢復API和機制僅在應用程序工具包(Mac OS X)中可用。

所以,我不知道是否可以使用NSErrorRecoveryAttempting即使它出現在文檔中進行定義(它看起來像這是一個尚未被複制後更新的UIKit文檔的區域來自AppKit的文檔)。

這裏是我在我的代碼處理錯誤:

NSError *error = nil; 
id result = [SomeClass doSomething:&error]; 

if (!result) { 
    NSLog(@"Do something failed: %@", error); 
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Something failed!" message:@"There was an error doing something." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; 
    [alert show]; 
    return; 
} 
+2

由於NSErrorRecoveryAttempting是一個非正式的協議,所以對象需要符合的就是實現這兩種方法。 iOS上的NSError肯定包含必要的屬性和常量。 Apple的評論顯然只是指在[這裏]描述的具體appkit方法(http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorRespondRecover/ErrorRespondRecover.html)和[here]( http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/HandleReceivedError/HandleReceivedError.html)。 – Anomie

+0

NSErrorRecoveryAttempting在iOS上工作得很好。你可以在NSError頭文件中看到它。缺少的是將錯誤傳遞給MacOS X上的警報的能力 - 通過在執行錯誤恢復的UIAlertView上添加一個類別非常容易,並且有幾個示例說明如何在此處執行此操作。 – quellish

0

我想反映UIKit中了AppKit的錯誤處理機制,主要是因爲我想借此響應鏈的優勢,向上向前的錯誤。我沒有完全測試,但目前看起來像下面。

它反映的AppKit非常接近,但可以重寫will/did鉤子來分別執行自定義錯誤表示和恢復。默認行爲是顯示用於演示的UIAlertView並使用psuedo-NSErrorRecoveryAttempting對象進行恢復。

@implementation UIResponder (ErrorHandling) 

- (void)presentError:(NSError *)error 
     completion:(void (^)(BOOL recovered))completion 
{ 
    if (nil == (error = [self willPresentError:error])) { 
     return; 
    } 
    if (self.nextResponder) { 
     [self.nextResponder presentError:error completion:completion]; 
     return; 
    } 

    // Code to create and show UIAlertView 
    // e.g. https://github.com/jayway/CWUIKit/blob/master/Classes/UIAlertView%2BCWErrorHandler.m 

    // The UIAlertViewDelegate calls didPresentError... 
} 

/* 
Override to customise the error object as in AppKit. 
You can also perform your own error presentation, and return nil to terminate the default handling. 
Custom error presentation UI should still call didPresentError... when dismissed 
*/ 
- (NSError *)willPresentError:(NSError *)error 
{ 
    return error; 
} 

/* 
Override to perform custom error recovery. 
*/ 
- (void)didPresentError:(NSError *)error optionIndex:(NSInteger)optionIndex completion:(void (^)(BOOL recovered))completion 
{ 
    id recoveryAttempter = [error recoveryAttempter]; 
    if ([recoveryAttempter respondsToSelector:@selector(attemptRecoveryFromError:optionIndex:completion:)]) { 
     [recoveryAttempter attemptRecoveryFromError:error optionIndex:optionIndex completion:completion]; 
    } 
} 

@end 
相關問題