ErrorKit 1.0.0

ErrorKit 1.0.0

测试已测试
语言编程语言 Obj-CObjective C
许可证 MIT
发布日期最后发布2017 年 4 月

Héctor Marqués 维护。



ErrorKit 1.0.0

  • Héctor Marqués Ranea

ErrorKit 是一个方便的 iOS 库,用于简化 NSError 的处理。它包括错误对象的创建、检查、展示和恢复。

它的目的是帮助您以更少、更简单的代码编写具有错误感知能力的应用程序。这通过将 Application Kit(见 Cocoa 错误处理编程指南)中提供的 error-respondererror-recovery 机制与一些新的具有错误感知方法的 API 相结合来实现。

示例

// Log error (if any)
MRLogError(error);
// Assert
MRNotErrorAssert(error);

访问错误值

NSString *helpAnchor = error.helpAnchor;
NSURLRequest *request = error.failingURLRequest;
NSArray *errors = error.detailedErrors;
// and so on...
// (supports most AFNetworking, AVFoundation, Core Data, Facebook SDK, JSONKit and Parse keys)

展示错误

if (!error.isCancelledError) {
    [[UIAlertView alertWithTitle:nil error:error] show];
}
// or you can rely on responder chain and do:
// [self presentError:error];

尝试错误恢复

if (error.code == NSURLErrorNotConnectedToInternet && error.isHTTPError) {
    MRErrorBuilder *builder = [MRErrorBuilder builderWithError:error];
    builder.localizedRecoverySuggestion = NSLocalizedString(@"Please check your internet connection.", nil);
    [builder addRecoveryOption:NSLocalizedString(@"Retry", nil)
                     withBlock:^(NSError *error) {
        [[HTTPClient sharedClient] resendRequest];
    }];
    [self presentError:builder.error];
}

将错误编码转换为字符串

NSString *debugString = [MRErrorFormatter debugStringWithDomain:error.domain code:error.code]; // e.g. NSURLErrorNetworkConnectionLost
NSString *localizedString = [MRErrorFormatter stringWithDomain:error.domain code:error.code]; // e.g. Connection Lost
// (supports most Accounts, Admob, AVFoundation, Core Data, Core Location, Facebook SDK, iAD, JSONKit, Map Kit, MessageUI, Parse, Security, Store Kit, TransitionKit and VeriJSON codes)

其他功能

处理 Facebook 错误

ErrorKit 为 Facebook 身份验证、请求权限和 API 请求错误提供了处理器。

// UIResponder+FacebookSDK:
- (BOOL)handleFacebookAuthError:(NSError *)error withLoginBlock:(void(^)(NSError *))loginBlock;
- (BOOL)handleFacebookRequestPermissionError:(NSError *)error;
- (BOOL)handleFacebookAPICallError:(NSError *)error withPermissionBlock:(void(^)(NSError *))permissionBlock andRetryBlock:(void(^)(NSError *))retryBlock;

安全地使用 JSON

序列化/反序列化
// NSJSONSerialization+JSONValues:
+ (NSData *)dataWithArray:(NSArray *)obj options:(NSJSONWritingOptions)opt error:(NSError **)errorPtr;
+ (NSData *)dataWithDictionary:(NSDictionary *)obj options:(NSJSONWritingOptions)opt error:(NSError **)errorPtr;
+ (NSArray *)arrayWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)errorPtr;
+ (NSDictionary *)dictionaryWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)errorPtr;
访问值

ErrorKit 还提供了从字典和数组中检索值的方法。

- (id)objectAtIndex:(NSUInteger)index withError:(NSError **)errorPtr;        
- (NSNumber *)numberForKey:(id)aKey withError:(NSError **)errorPtr;
- (NSString *)stringForKey:(id)aKey withError:(NSError **)errorPtr;
- (NSArray *)arrayForKey:(id)aKey withError:(NSError **)errorPtr;
- (NSDictionary *)dictionaryForKey:(id)aKey withError:(NSError **)errorPtr;

或者您可以使用这些方法的块版本。

- (BOOL)objectAtIndex:(NSUInteger)index block:(void(^)(id object, NSError *error))block;
- (BOOL)numberForKey:(id)aKey block:(void(^)(NSNumber *number, NSError *error))block;
// etc.
操作可变集合
// NSMutableArray+JSONValues:
- (BOOL)addObject:(id)anObject withError:(NSError **)errorPtr;
- (BOOL)removeObjectAtIndex:(NSUInteger)index withError:(NSError **)errorPtr;
- (BOOL)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject error:(NSError **)errorPtr;
// etc.

// NSMutableDictionary+JSONValues:
- (BOOL)setNumber:(NSNumber *)aNumber forKey:(id<NSCopying>)aKey withError:(NSError **)errorPtr;
- (BOOL)setString:(NSString *)aString forKey:(id<NSCopying>)aKey withError:(NSError **)errorPtr;
// etc.

请参阅 ErrorKit-ExampleCoreData-Example 项目,或在网上浏览 文档以获取更多信息。

安装

  1. ErrorKit 文件夹拖放到您的项目中。
  2. 添加 #import "ErrorKitDefines.h",或者在代码中某处定义 ERROR_KIT_CORE 和以下一些常量来自定义启用功能(例如在 -Prefix.pch 文件中):ERROR_KIT_ACCOUNTSERROR_KIT_ADDITIONSERROR_KIT_ADMOBERROR_KIT_AFNETWORKINGERROR_KIT_AVFOUNDATIONERROR_KIT_CORE_DATAERROR_KIT_CORE_LOCATIONERROR_KIT_FACEBOOKERROR_KIT_HTTPERROR_KIT_JSON_KITERROR_KIT_JSON_VALUESERROR_KIT_MAP_KITERROR_KIT_MESSAGE_UIERROR_KIT_NSEXCEPTIONERROR_KIT_PARSEERROR_SECURITYERROR_KIT_STORE_KITERROR_KIT_TRANSITION_KITERROR_KIT_IADERROR_KIT_UI_KIT 和/或 ERROR_KIT_VERI_JSON
  3. 最后添加 #import "ErrorKit.h"

许可证

ErrorKit 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。

替代方案

实现类似错误处理和展示机制的其它项目