私は上向きにエラーを転送するために、レスポンダチェーンを利用したい主な理由は、UIKitの中のAppKitのエラー処理メカニズムを反映しようとしています。私はこれを完全にはテストしていませんが、現時点では以下のようになります。
それはかなり密接のAppKitを反映しているが、意志は/フックはそれぞれカスタムエラープレゼンテーションとリカバリを実行するためにオーバーライドすることができませんでした。デフォルトの動作では、プレゼンテーションのためUIAlertViewを示し、回復のための擬似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
ある時点で、私はUIAlertViewなどを使ってNSErrorを提示して処理するコードを書いていましたが、実際には 'recoveryAttempter'というエラーが発生しているので、本当に正しいかどうかわかりません。 – Anomie