随着项目越来越大,项目model 更是数不胜数,当我们遇到一个model 想去转换另一个model 的时候,经常会写一些非常恶心的代码。
每次去看都非常的影响心情,所以决定干掉他。来吧 baby
先看看这样的代码有多么恶心,来
//动物怎么变成人
Animal *animal = [[Animal alloc]init];
animal.animalName = @"猿";
animal.animalAge = @"20";
animal.animalSex = @"男";
animal.animalPower = @"Strong";
//long long wait
//变成人
People *people = [[People alloc]init];
people.peopleName = @"人";
people.peopleAge = animal.animalAge;
people.peopleSex = animal.animalSex;
people.peoplePower = animal.animalPower;
NSLog(@"animal %@-%@",animal.animalName,animal.animalPower);
NSLog(@"people %@-%@",people.peopleName,people.peoplePower);
这个世界就是这么来的,我也不想继承,继承带来的后果无法预知。就这样简简单单日子也是过,but 每天看着没有洗的碗筷在哪里放着,实在受不了。
所以打扫了一下,结果就是这样了:
//动物怎么变成人
[[BaseConversion getInstance] set_modelKeyValue:[People class]
andModel:[Animal class]
params:@{@"animalAge":@"peopleAge",
@"animalSex":@"peopleSex",
@"animalPower":@"peoplePower"
}];
Animal *animal = [[Animal alloc]init];
animal.animalName = @"猿";
animal.animalAge = @"20";
animal.animalSex = @"男";
animal.animalPower = @"Strong";
//long long wait
//变成人
People *people = [People set_keysAndValuesToModel:animal];
people.peopleName = @"人";
NSLog(@"animal %@-%@",animal.animalName,animal.animalPower);
NSLog(@"people %@-%@",people.peopleName,people.peoplePower);
好点了没,不要书行数好吗?
来看看一个方法啊
[[BaseConversion getInstance] set_modelKeyValue:[People class]
andModel:[Animal class]
params:@{@"animalAge":@"peopleAge",
@"animalSex":@"peopleSex",
@"animalPower":@"peoplePower"
}];
定义了转换model的属性key,一一对应,双向转换。
使用的时候只用一行就好了
People *people = [People set_keysAndValuesToModel:animal];
简单实用,代码一点点。下边是源码:喜欢的点个赞,我会在把她丰富一些给大家下载的。
+ (instancetype)set_keysAndValuesToModel:(id)object{
NSString *firstModel = NSStringFromClass([self class]);
NSString *secondModel = NSStringFromClass([object class]);
NSString *dicKey = [NSString stringWithFormat:@"%@%@%@",firstModel,ConversionKey,secondModel];
NSDictionary *dic = [[BaseConversion getInstance].modelDic valueForKey:dicKey];
if (dicKey == nil) {
return nil;
}
id toModel = [[[self class] alloc]init];
if ([NSStringFromClass([self class]) isEqualToString:NSStringFromClass([object class])]) {
NSArray *propertys = [self getPropertys];
for (NSString *perperty in propertys) {
SEL getSel = [self creatGetterWithPropertyName:perperty];
if ([object respondsToSelector:getSel]) {
SEL sel = [self creatSetterWithPropertyName:perperty];
if ([toModel respondsToSelector:sel]) {
[toModel performSelectorOnMainThread:sel withObject:[self getValue:object property:perperty] waitUntilDone:[NSThread isMainThread]];
}
}
}
}else{
for (NSString *key in dic) {
NSString *keyV = [dic valueForKey:key];
SEL getSel = [self creatGetterWithPropertyName:key];
SEL getSelV = [self creatGetterWithPropertyName:keyV];
if ([object respondsToSelector:getSel]) {
SEL sel = [self creatSetterWithPropertyName:keyV];
if ([toModel respondsToSelector:sel]) {
[toModel performSelectorOnMainThread:sel withObject:[self getValue:object property:key] waitUntilDone:[NSThread isMainThread]];
}
}else if ([object respondsToSelector:getSelV]) {
SEL sel = [self creatSetterWithPropertyName:key];
if ([toModel respondsToSelector:sel]) {
[toModel performSelectorOnMainThread:sel withObject:[self getValue:object property:keyV] waitUntilDone:[NSThread isMainThread]];
}
}
}
}
return toModel;
}
- (SEL)creatSetterWithPropertyName: (NSString *) propertyName{
if (propertyName.length > 1) {
NSString *str = [propertyName substringToIndex:1];
NSString *strLast = [propertyName substringFromIndex:1];
str = str.capitalizedString;
propertyName = [NSString stringWithFormat:@"%@%@",str,strLast];
}else{
propertyName = propertyName.capitalizedString;
}
propertyName = [NSString stringWithFormat:@"set%@:", propertyName];
return NSSelectorFromString(propertyName);
}
- (SEL) creatGetterWithPropertyName: (NSString *) propertyName{
return NSSelectorFromString(propertyName);
}
- (NSString *)getValue:(id)object property:(NSString *)property{
//获取get方法
SEL getSel = [self creatGetterWithPropertyName:property];
if ([object respondsToSelector:getSel]) {
NSMethodSignature *signature = [object methodSignatureForSelector:getSel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:object];
[invocation setSelector:getSel];
NSObject *__unsafe_unretained returnValue = nil;
[invocation invoke];
[invocation getReturnValue:&returnValue];
return [NSString stringWithFormat:@"%@",returnValue];
}
return nil;
}
- (NSArray *)getPropertys{
NSMutableArray *array = [NSMutableArray array];
u_int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
const char *propertyName = property_getName(properties[i]);
NSString *str = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
[array addObject:str];
}
return array;
}
网友评论