美文网首页
iOS 模型拷贝

iOS 模型拷贝

作者: no1ever | 来源:发表于2020-11-11 11:19 被阅读0次

    模型对象深复制,new 一个新的模型对象,将旧模型的各个属性值全部赋值过去

    #import <objc/runtime.h>
    
    #pragma mark 模型拷贝  在<YYModel>时会多出4个私有属性
    //hash  superclass  description  debugDescription
    +(id)changeModle:(id)model modelClassname:(NSString *)classname
    {
        return [self changeModle:model modelClassname:classname removeKey:nil];
    }
    
    #pragma mark 模型拷贝  在<YYModel>时会多出4个私有属性
    //hash  superclass  description  debugDescription
    //rKey 排除某个key
    +(id)changeModle:(id)model modelClassname:(NSString *)classname removeKey:(NSArray *)rKeyArray
    {
        unsigned int count;
        objc_property_t *properties=class_copyPropertyList([model class], &count);
        
        /*
         unsigned int ivarCount;
         Ivar *ivars = class_copyIvarList([model class], &ivarCount);
         */
        id backModel=[[NSClassFromString(classname) alloc]init];
        
        unsigned int count_new;
        objc_property_t *properties_new = class_copyPropertyList([backModel class], &count_new);
        
        //    if (count > count_new) {
        //        count = count_new;
        //    }
        
        for(int i =0; i < count; i++) {
            objc_property_t property = properties[i];
            //获取属性的名称C语言字符串
            const char *cName = property_getName(property);
            //转换为Objective C字符串
            NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
            
            
            //去掉私有属性
            //方法一
            if ([name isEqualToString:@"hash"] || [name isEqualToString:@"superclass"] || [name isEqualToString:@"description"] || [name isEqualToString:@"debugDescription"]) {
                continue;
            }
            
            /*
             //方法二  错误 不可以,属性前有 _(eg:_id)
             BOOL flag = YES;
             for (int j = 0; j < ivarCount; ++j) {
             const char *iName = ivar_getName(ivars[j]);
             NSString *_name = [NSString stringWithUTF8String:iName];
             if ([_name isEqualToString:name]) {
             break;
             }else{
             if (j == ivarCount - 1) {
             flag = NO;
             }
             }
             }
             if (!flag) {
             continue;
             }
             */
            
            
            id value= [model valueForKey:name];
            if (![value isKindOfClass:[NSNull class]] && value != nil) {
                //            [backModel setValue:value forKey:name];
                
                for (int j = 0; j < count_new; ++j) {
                    
                    objc_property_t property_new = properties_new[j];
                    //获取属性的名称C语言字符串
                    const char *cName_new = property_getName(property_new);
                    //转换为Objective C字符串
                    NSString *name_new = [NSString stringWithCString:cName_new encoding:NSUTF8StringEncoding];
                    if ([rKeyArray containsObject:name]) {
                        continue;
                    }
                    if ([name isEqualToString:name_new]) {
                        //保证对相同属性赋值
                        [backModel setValue:value forKey:name];
                        break;
                    }
                    
                }
                
            }
            
        }
        free(properties_new);
        free(properties);
        return backModel;
    }
    //移除模型某个属性值 注意modelCustomPropertyMapper, 解决key转换
    /*
     + (NSDictionary<NSString *,id> *)modelCustomPropertyMapper{
     return @{@"nVer":@"newVer"};
     }
     */
    +(id)changeModel:(id)model removeKey:(NSArray *)rKeyArray modelCustomPropertyMapper:(NSDictionary *)mDic{
        
        unsigned int count;
        objc_property_t *properties=class_copyPropertyList([model class], &count);
        
    //    id backModel=[[NSClassFromString(NSStringFromClass([model class])) alloc]init];
        id backModel = [NSMutableDictionary dictionary];
        for(int i =0; i < count; i++) {
            objc_property_t property = properties[i];
            //获取属性的名称C语言字符串
            const char *cName = property_getName(property);
            //转换为Objective C字符串
            NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
            //去掉私有属性
            //方法一
            if ([name isEqualToString:@"hash"] || [name isEqualToString:@"superclass"] || [name isEqualToString:@"description"] || [name isEqualToString:@"debugDescription"]) {
                continue;
            }
            id value= [model valueForKey:name];
            if (![value isKindOfClass:[NSNull class]] && value != nil) {
                if ([rKeyArray containsObject:name]) {
                    continue;
                }
                NSString *mName = [mDic valueForKey:name];
                if (mName) {
                    name = mName;
                }
                [backModel setValue:value forKey:name];
            }
        }
        free(properties);
        return backModel;
    }
    

    ps: YYModel 中也提供模型复制的方法

    相关文章

      网友评论

          本文标题:iOS 模型拷贝

          本文链接:https://www.haomeiwen.com/subject/woalbktx.html