sto cercando di rispecchiare il meccanismo di gestione degli errori di AppKit in UIKit, soprattutto perché voglio approfittare della catena risponditore per inoltrare gli errori verso l'alto. Non l'ho testato completamente, ma al momento sembra di seguito.
Rispecchia AppKit abbastanza da vicino, ma gli hook di volontà/di fatto possono essere sovrascritti per eseguire rispettivamente la presentazione e il ripristino degli errori personalizzati. Il comportamento predefinito consiste nel mostrare un UIAlertView per la presentazione e utilizzare un oggetto psuedo-NSErrorRecoveryAttempting per il ripristino.
@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
A un certo punto ho scritto codice per fare esattamente questo (gestisco presentando NSError usando UIAlertView ecc.), Ma devo ancora vedere un errore che in realtà ha un 'recoveryAttempter' quindi non so se è davvero giusto. – Anomie