美文网首页
YYModel的一种使用方式 2023-04-26 周三

YYModel的一种使用方式 2023-04-26 周三

作者: 勇往直前888 | 来源:发表于2023-04-25 16:42 被阅读0次

    简介

    在OC工程中,字典转模型是很重要的一个内容,基本上每一个工程都要用到。这块内容基本上会用到第三方库,而本人自从接触到YYModel之后,就再也没有更换过,一直用。
    以前都是直接使用YYModel,而这次采用了基类的封装使用方式,感觉更好。
    以后,提供一个Model的基类在整个工程中使用将成为标准操作。

    封装方式

    头文件:

    @interface WGBBaseModel : NSObject <NSCopying, NSCoding>
    
    /// 通过JSON得到实例
    + (instancetype)instanceWithJSON:(id)json targetClass:(Class)targetClass;
    
    /// 通过JSON得到实例数组
    + (NSArray *)arrayWithJSON:(id)json targetClass:(Class)targetClass;
    
    @end
    

    源文件:

    #import "WGBBaseModel.h"
    #import <YYModel/YYModel.h>
    
    @implementation WGBBaseModel
    
    #pragma mark - instance
    /// 通过JSON得到实例
    + (instancetype)instanceWithJSON:(id)json targetClass:(Class)targetClass {
        return [targetClass yy_modelWithJSON:json];
    }
    
    /// 通过JSON得到实例数组
    + (NSArray *)arrayWithJSON:(id)json targetClass:(Class)targetClass {
        return [NSArray yy_modelArrayWithClass:targetClass json:json];
    }
    
    #pragma mark - NSCopying, NSCoding
    
    - (void)encodeWithCoder:(NSCoder *)coder {
        [self yy_modelEncodeWithCoder:coder];
    }
    
    - (instancetype)initWithCoder:(NSCoder *)coder {
        if (self = [super init]) {
            [self yy_modelInitWithCoder:coder];
        }
        return self;
    }
    
    - (id)copyWithZone:(NSZone *)zone {
        return [self yy_modelCopy];
    }
    
    - (NSUInteger)hash {
        return [self yy_modelHash];
    }
    
    - (BOOL)isEqual:(id)object {
        return [self yy_modelIsEqual:object];
    }
    
    @end
    

    协议

    • 也就是拷贝协议和编码协议<NSCopying, NSCoding>

    • 自定义类要用copy方法,要缓存本地,就需要实现这两个协议

    • 大多数时候是不需要的,但是一旦用到,就会非常繁琐

    • 用YYModel实现,非常简单。所以这部分内容写在基类中,价值非常大

    • 相等比较,也是非常有用的。如果不用YYModel,自定义Model的相等就需要自己实现,非常麻烦。

    - (NSUInteger)hash {
        return [self yy_modelHash];
    }
    
    - (BOOL)isEqual:(id)object {
        return [self yy_modelIsEqual:object];
    }
    
    • 这些基础的内容在基类中用YYModel实现之后,自定义的Model就可以缓存本地,进行比较,进行复制,进行比较等等都可以随意使用,非常方便。

    字典转模型

    • 网络接口过来,响应一般是字典或者字典的数组。直接使用,记忆字段很不方便。所以OC一般都要转换成模型Model,方便使用。

    • 字典转单个Model,或者字典的数组转Model的数组,是最多的两种使用场景,所以在基类中进行封装一下。

    /// 通过JSON得到实例
    + (instancetype)instanceWithJSON:(id)json targetClass:(Class)targetClass {
        return [targetClass yy_modelWithJSON:json];
    }
    
    /// 通过JSON得到实例数组
    + (NSArray *)arrayWithJSON:(id)json targetClass:(Class)targetClass {
        return [NSArray yy_modelArrayWithClass:targetClass json:json];
    }
    

    模型转字典

    • 一般用在给接口提供参数,使用场景很少。

    • 给接口提供参数,还是直接使用字典方便。

    • 所以,模型转字典这部分不封装。

    例子

    字典转单个模型

    • 模型定义,用上自定义的基类,隐藏了YYModel的使用
    @interface WGBUserInfoModel : WGBBaseModel
    
    /// userId
    @property(copy, nonatomic) NSString *userId;
    
    /// 用户名
    @property(copy, nonatomic) NSString *userName;
    
    /// barCode
    @property(copy, nonatomic) NSString *barCode;
    
    /// 版本号
    @property(assign, nonatomic) NSInteger versionCode;
    
    @end
    
    • 直接使用封装的方法进行字典转模型,不需要直接调用YYModel的方法
    /// 字典转模型
    WGBUserInfoModel *userInfoModel = [WGBUserInfoModel instanceWithJSON:response targetClass:[WGBUserInfoModel class]];
    

    字典数组转模型数组

    • 模型定义
    @interface WGBFinePhotoModel : WGBBaseModel
    
    /// 已有的拍照数量
    @property(assign, nonatomic) NSInteger hasPhotoNumber;
    
    /// 总共需要拍照的数量;默认拍照数量
    @property(assign, nonatomic) NSInteger totalPhotoNumber;
    
    /// 已有照片的url列表
    @property(copy, nonatomic) NSArray<NSString *> *picList;
    
    /// 备注
    @property(copy, nonatomic) NSString *remark;
    
    @end
    
    • 转模型数组
    /// 转模型数组
    NSArray<WGBFinePhotoModel *> *fineList = [WGBFinePhotoModel arrayWithJSON:response targetClass:[WGBFinePhotoModel class]];
    

    相关文章

      网友评论

          本文标题:YYModel的一种使用方式 2023-04-26 周三

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