ErrorKit 是一个方便的 iOS 库,用于简化 NSError
的处理。它包括错误对象的创建、检查、展示和恢复。
它的目的是帮助您以更少、更简单的代码编写具有错误感知能力的应用程序。这通过将 Application Kit(见 Cocoa 错误处理编程指南)中提供的 error-responder 和 error-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)
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;
// 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-Example 和 CoreData-Example 项目,或在网上浏览 文档以获取更多信息。
#import "ErrorKitDefines.h"
,或者在代码中某处定义 ERROR_KIT_CORE
和以下一些常量来自定义启用功能(例如在 -Prefix.pch 文件中):ERROR_KIT_ACCOUNTS
、ERROR_KIT_ADDITIONS
、ERROR_KIT_ADMOB
、ERROR_KIT_AFNETWORKING
、ERROR_KIT_AVFOUNDATION
、ERROR_KIT_CORE_DATA
、ERROR_KIT_CORE_LOCATION
、ERROR_KIT_FACEBOOK
、ERROR_KIT_HTTP
、ERROR_KIT_JSON_KIT
、ERROR_KIT_JSON_VALUES
、ERROR_KIT_MAP_KIT
、ERROR_KIT_MESSAGE_UI
、ERROR_KIT_NSEXCEPTION
、ERROR_KIT_PARSE
、ERROR_SECURITY
、ERROR_KIT_STORE_KIT
、ERROR_KIT_TRANSITION_KIT
、ERROR_KIT_IAD
、ERROR_KIT_UI_KIT
和/或 ERROR_KIT_VERI_JSON
。#import "ErrorKit.h"
。ErrorKit 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。
实现类似错误处理和展示机制的其它项目