问题:在系统中的网络请求的block处理中,有2种errorCode和errorcode 格式,errortext和errorText同理,为了避免写错,现在提出以下技术方案。
1.如果以前的代码保证正确,则可以给NSDictionary增加category 方法,以后获取errorcode,errortext统一使用errorCodeForKey,errorTextForKey方法。
缺点:系统中会出现2中方式的的取值
优点:安全,效率高。
import <Foundation/Foundation.h>
@interface NSDictionary (ErrorCodeAndErrorText)
- (NSString*)errorCodeForKey:(id)key;
- (NSString*)errorTextForKey:(id)key;
@end
import "NSDictionary+ErrorCodeAndErrorText.h"
import <objc/message.h>
@implementation NSDictionary (ErrorCodeAndErrorText)
- (NSString*)errorCodeForKey:(id)key{
id value = [self objectForKey:key];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self objectForKey:@"errorcode"];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self objectForKey:@"errorCode"];
if (value != nil && value != [NSNull null])
{
return value;
}
return nil;
} - (NSString*)errorTextForKey:(id)key{
id value = [self objectForKey:key];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self objectForKey:@"errortext"];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self objectForKey:@"errorText"];
if (value != nil && value != [NSNull null])
{
return value;
}
return nil;
}
@end
2.在+(id)changeType:(id)myObj方法中针对errorText errorCode的处理方法。
缺点:这个字典稍显混乱,遍历字典效率稍微低。
优点:对老代码兼容。
+(NSDictionary *)errorKeyDic:(NSDictionary *)myDic
{
NSArray *keyArr = [myDic allKeys];
NSMutableDictionary *resDic = [NSMutableDictionary dictionaryWithDictionary:myDic];
if([keyArr containsObject:@"errorcode"]){
[resDic setObject:[myDic objectForKey:@"errorcode"] forKey:@"errorCode"];
}else if([keyArr containsObject:@"errorCode"]){
[resDic setObject:[myDic objectForKey:@"errorCode"] forKey:@"errorcode"];
}
if([keyArr containsObject:@"errortext"]){
[resDic setObject:[myDic objectForKey:@"errortext"] forKey:@"errorText"];
}else if([keyArr containsObject:@"errorText"]){
[resDic setObject:[myDic objectForKey:@"errorText"] forKey:@"errortext"];
}
return resDic;
}
3.利用runtime,在拦截NSDictionary的objectForKey:方法;
缺点:会修改全局,慎用!!!
优点:不需要改动其他地方。
import <Foundation/Foundation.h>
@interface NSDictionary (ErrorCodeAndErrorText)
@end
import "NSDictionary+ErrorCodeAndErrorText.h"
import <objc/runtime.h>
@implementation NSDictionary (ErrorCodeAndErrorText)
-
(void)load
{
//NSDictnary的真实类型__NSDictionaryI
Class class = objc_getClass("__NSDictionaryI");SEL originalSelector = @selector(objectForKey:);
SEL swizzledSelector = @selector(dw_objectForKey:);Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (id)errorCodeForKey:(id)key{
id value = [self dw_objectForKey:key];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self dw_objectForKey:@"errorcode"];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self dw_objectForKey:@"errorCode"];
if (value != nil && value != [NSNull null])
{
return value;
}
return nil;
} - (id)errorTextForKey:(id)key{
id value = [self dw_objectForKey:key];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self dw_objectForKey:@"errortext"];
if (value != nil && value != [NSNull null])
{
return value;
}
value = [self dw_objectForKey:@"errorText"];
if (value != nil && value != [NSNull null])
{
return value;
}
return nil;
}
- (nullable id)dw_objectForKey:(id)key
{
if([key isEqualToString:@"errorCode"] || [key isEqualToString:@"errorcode"]){
return [self errorCodeForKey:@"errorcode"];
}else if([key isEqualToString:@"errorText"] || [key isEqualToString:@"errorText"]){
return [self errorTextForKey:@"errorText"];
}
return [self dw_objectForKey:key];
}
@end