問題:在系統(tǒng)中的網(wǎng)絡請求的block處理中哟绊,有2種errorCode和errorcode 格式,errortext和errorText同理号枕,為了避免寫錯懂更,現(xiàn)在提出以下技術(shù)方案。
1.如果以前的代碼保證正確竹握,則可以給NSDictionary增加category 方法画株,以后獲取errorcode,errortext統(tǒng)一使用errorCodeForKey,errorTextForKey方法谓传。
缺點:系統(tǒng)中會出現(xiàn)2中方式的的取值
優(yōu)點:安全蜈项,效率高。
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的處理方法续挟。
缺點:這個字典稍顯混亂紧卒,遍歷字典效率稍微低。
優(yōu)點:對老代碼兼容诗祸。
+(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:方法;
缺點:會修改全局,慎用V甭2└觥!
優(yōu)點:不需要改動其他地方际乘。
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