[iOS]YYModel用法总结

本文介绍了iOS开发中,从 MJExtension 转向使用 YYModel 的原因,并通过创建 YYAuthor 和 YYBook 模型类,展示了 YYModel 的基本使用方法,包括模型的属性定义和转换操作。
摘要由CSDN通过智能技术生成

笔者之前一直在用MJExtension来做网络数据模型转换,最近发现这个库在某些场景下转换会出现问题,然后就尝试了使用YYModel。YYModel很久之前就听说过,源代码写得很精妙,也有很多开发者在用。这里简单通过demo总结下它的用法。

 

新建两个model:YYAuthor和YYBook。

YYAuthor.h

//
//  YYAuthor.h
//  YYModelDemo
//
//  Created by mac on 2019/05/01.
//  Copyright © 2019 mac All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface YYAuthor : NSObject<NSCopying,NSCoding,NSMutableCopying>

@property (nonatomic,copy) NSString *name;
@property (nonatomic,strong) NSDate *birthDay;


@end

NS_ASSUME_NONNULL_END

YYAuthor.m

//
//  YYAuthor.m
//  YYModelDemo
//
//  Created by mac on 2019/05/01
//  Copyright © 2019 mac All rights reserved.
//

#import "YYAuthor.h"
#import <NSObject+YYModel.h>

@implementation YYAuthor

/**
 If the key in JSON/Dictionary does not match to the model's property name,
 implements this method and returns the additional mapper.
 */

+ (NSDictionary *)modelCustomPropertyMapper
{
    return @{@"birthDay":@"birthday"};
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [self yy_modelEncodeWithCoder:coder];
}

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

- (id)copyWithZone:(NSZone *)zone
{
    return [self yy_modelCopy];
}


- (id)mutableCopyWithZone:(nullable NSZone *)zone
{
    YYAuthor *copy;
    if ((copy = [[[self class] alloc] init])) {
        [copy setValue:self.name forKey:@"name"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值