Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm trying to mirror AppKit's error handling mechanism in UIKit, mainly because I want to take advantage of the responder chain to forward errors upwards. I haven't tested this fully, but at the moment it's looking like below.</p> <p>It reflects AppKit pretty closely, but the will/did hooks can be overridden to perform custom error presentation and recovery respectively. The default behaviour is to show a UIAlertView for presentation and use a psuedo-NSErrorRecoveryAttempting object for recovery.</p> <pre><code>@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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload