RMErrorRecoveryAttempter 0.0.1

RMErrorRecoveryAttempter 0.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
已发布最后发布2014年12月

未申报维护。



  • 作者
  • Keith Duncan, Damien DeVille 和 James Beith

RMErrorRecoveryAttempter 是一个遵守 NSErrorRecoveryAttempting 非正式协议的类,它利用块的功能,允许您为错误提供恢复选项。

阅读 Realmac 博客上的 Cocoa Error Handling and Recovery 博客文章,了解更多错误处理技巧。

示例项目

示例项目,RMErrorRecoveryAttempterSampleProject,是一个 iOS 应用,可以创建锁定和解锁的项。如果您滑动删除一个锁定的项,将会创建一个错误。这个错误的用户信息字典中包含一个 RMErrorRecoveryAttempter 对象,该对象有两个恢复选项,每个选项都有一个标题和块对象。恢复选项的标题用于填充提醒框中按钮,当点击按钮时,对应的块对象将被执行。这些恢复选项返回 YESNO,以通知调用者是否重新发送失败的原始消息。

RMErrorRecoveryAttempter *errorRecoveryAttempter = [[RMErrorRecoveryAttempter alloc] init];
[errorRecoveryAttempter addRecoveryOptionWithLocalizedTitle:NSLocalizedString(@"Don\u2019t Unlock", @"RMMasterViewController delete locked item error don't unlock recovery option") recoveryBlock:^ BOOL (void) {
    // Do not attempt to recover from the error. Return NO to inform the caller that they should not resend the message that failed.
    return NO;
}];
[errorRecoveryAttempter addRecoveryOptionWithLocalizedTitle:NSLocalizedString(@"Unlock & Delete", @"RMMasterViewController delete locked item error unlock & delete recovery option") recoveryBlock:^ BOOL (void) {
    // Do the required work to recover from the error; unlock the item.
    [item setLocked:NO];
    // Return YES to inform the caller that they should resend the message that failed, not that the original functionality of that message has been performed.
    return YES;
}];
/*
    The `userInfo` dictionary populated with our localized title and messages strings and `RMErrorRecoveryAttempter`. If you have an underlying 
    error, for example an error from a failed `-[NSManagedObjectContext save:]`, you can include it under the `NSUnderlyingErrorKey`.
 */
NSDictionary *userInfo = @{
    NSLocalizedDescriptionKey : NSLocalizedString(@"Cannot delete a locked item", @"RMMasterViewController delete locked item error description"),
    NSLocalizedRecoverySuggestionErrorKey : NSLocalizedString(@"This item cannot be deleted because it is currently locked. Would you like to Unlock & Delete this item?", @"RMMasterViewController delete locked item error recovery suggestion"),
    NSRecoveryAttempterErrorKey : errorRecoveryAttempter,
    NSLocalizedRecoveryOptionsErrorKey : [errorRecoveryAttempter recoveryOptions],
};
*errorRef = [NSError errorWithDomain:RMErrorRecoveryAttempterSampleProjectErrorDomain code:RMErrorRecoveryAttempterSampleProjectErrorCodeLockedItem userInfo:userInfo];

使用 UIResponder+RMErrorRecovery 类别来显示提醒框。如果完成处理器的 recovered 参数是 YES,则用户选择了恢复路径,因此重新发送删除项的消息。

- (void)rm_presentError:(NSError *)error completionHandler:(void (^)(BOOL recovered))completionHandler;

在 OS X 上,您可以使用以下两个 AppKit 方法来显示错误。

- (BOOL)presentError:(NSError *)error;
- (void)presentError:(NSError *)error modalForWindow:(NSWindow *)window delegate:(id)delegate didPresentSelector:(SEL)didPresentSelector contextInfo:(void *)contextInfo;

要求

  • iOS 5.0 及以上版本,或 OS X 10.7 及以上版本
  • LLVM 编译器 4.0 及以上
  • ARC

如果您的项目没有使用 ARC,您需要在 RMErrorRecoveryAttempterUIResponder+RMErrorRecovery 源文件上设置 -fobjc-arc 编译器标志。要在 Xcode 中设置这些,请转到您的活动目标并选择“构建阶段”选项卡。展开“编译源代码”部分,选择提到的源文件,按 Enter,然后插入 -fobjc-arc

联系

有关此项目,请联系 James Beith,[email protected]

致谢

Keith Duncan, @keith_duncan
Damien DeVille, @DamienDeVille
James Beith, @jamesbeith

许可证

有关更多信息,请参阅 LICENSE 文件。