使用YYModel 字典转换模型

使用YYModel 字典转换模型,第1张

1、模型中包含数组模型,在.m文件中加入modelContainerPropertyGenericClass方法


@interface XMallSpecificationModel : NSObject <YYModel>

@property (nonatomic, copy) NSString *carSpecificationName;
@property (nonatomic, copy) NSString *iconPicFileUrl;

@end

@interface XMallOrderModel : NSObject <YYModel>
@property (nonatomic, copy) NSString *orderCode;
@property (nonatomic, copy) NSArray<XMallSpecificationModel*> *optionalSpecificationList;

@end

//.m文件
+ (NSDictionary *)modelContainerPropertyGenericClass {

 return @{@"optionalSpecificationList":[XMallSpecificationModel class]};
}

2、修改模型中的值

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic{
    unsigned int outCount = 0;
    Ivar *ivars = class_copyIvarList([self class], &outCount);
    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
        id value = [self valueForKeyPath:key];
        if ([type containsString:@"NSString"]&&[self isEmptyStr:value]) {
            [self setValue:@"" forKey:key];
        }

    }
    // 释放内存!
    free(ivars);
    return YES;
}

4、归档与解挡

- (void)encodeWithCoder:(NSCoder *)aCoder {
 [self yy_modelEncodeWithCoder:aCoder];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
  self = [super init];
  return [self yy_modelInitWithCoder:aDecoder];
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/997030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存