笔者之前一直在用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;@endNS_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"];[copy setValue:self.birthDay forKey:@"birthDay"];}return copy;
}- (NSUInteger)hash
{return [self yy_modelHash];
}- (BOOL)isEqual:(id)object{return [self yy_modelIsEqual:object];
}@end

YYBook.h

//
//  YYBook.h
//  YYModelDemo
//
//  Created by mac on 2019/05/01.
//  Copyright © 2019 mac All rights reserved.
//#import <Foundation/Foundation.h>
#import "YYAuthor.h"NS_ASSUME_NONNULL_BEGIN@interface YYBook : NSObject<NSCoding,NSCopying,NSMutableCopying>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSUInteger page;
@property (nonatomic, strong) YYAuthor *author;
@property (nonatomic, strong) NSArray <YYAuthor *> *authors;
@property (nonatomic, copy) NSString *publish;@endNS_ASSUME_NONNULL_END

YYBook.m

//
//  YYBook.m
//  YYModelDemo
//
//  Created by mac on 2019/05/01.
//  Copyright © 2019 mac All rights reserved.
//#import "YYBook.h"
#import <NSObject+YYModel.h>
@implementation YYBook/**
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 @{@"page":@"pages"};
}/**If the property is a container object, such as NSArray/NSSet/NSDictionary,implements this method and returns a property->class mapper, tells which kind ofobject will be add to the array/set/dictionary.*/+ (NSDictionary *)modelContainerPropertyGenericClass {return @{@"authors" : [YYAuthor class]};
}// NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{[self yy_modelEncodeWithCoder:coder];
}- (instancetype)initWithCoder:(NSCoder *)aDecoder
{self = [super init];return [self yy_modelInitWithCoder:aDecoder];
}//NSCopying
- (id)copyWithZone:(NSZone *)zone
{return [self yy_modelCopy];
}- (id)mutableCopyWithZone:(nullable NSZone *)zone
{YYBook *copy;if ((copy = [[[self class] alloc] init])) {[copy setValue:self.name forKey:@"name"];[copy setValue:@(self.page) forKey:@"pages"];[copy setValue:self.author forKey:@"author"];[copy setValue:self.authors forKey:@"authors"];}return copy;
}// hash
- (NSUInteger)hash
{return [self yy_modelHash];
}// isEqual
- (BOOL)isEqual:(id)object{return [self yy_modelIsEqual:object];
}/**All the properties in blacklist will be ignored in model transform process.Returns nil to ignore this feature.@return An array of property's name.*/
+ (nullable NSArray<NSString *> *)modelPropertyBlacklist
{return @[@"publish"];
}/**If a property is not in the whitelist, it will be ignored in model transform process.Returns nil to ignore this feature.@return An array of property's name.*/
+ (nullable NSArray<NSString *> *)modelPropertyWhitelist
{return @[@"name",@"author",@"authors"];
}
@end

使用方法:

ViewController.m

//
//  ViewController.m
//  YYModelDemo
//
//  Created by mac on 2019/05/01.
//  Copyright © 2019 mac All rights reserved.
//#import "ViewController.h"
#import <YYModel/YYModel.h>
#import "YYAuthor.h"
#import "YYBook.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];///>Json->modelNSString *jsonStr = @"{\"name\":\"Harry Potter\",\"pages\":256,\"author\":{\"name\":\"J.K.Rowling\",\"birthDay\":\"1975-07-31\"}}";YYBook *B = [YYBook yy_modelWithJSON:jsonStr];YYBook *b =  [B mutableCopy];if ([b isEqual:B]) {// 对象内容是相等的NSLog(@"b is equal toB");} else {NSLog(@"b is not requal toB");}YYAuthor *ba = B.author;YYAuthor *baa = [ba mutableCopy];if ([ba isEqual:baa]) {// 对象内容是相等的NSLog(@"ba is equal baa");} else {NSLog(@"ba is not requal baa");}///>>NSDictionary->modelNSDictionary *dic = @{@"name":@"Harry Potter",@"pages":@(256),@"author":@{@"name":@"J.K.Rowling",@"birthday":@"1975-07-31"},@"publish":@"BeijingUniversity"};YYBook *book = [YYBook yy_modelWithDictionary:dic];///>>>container propertyNSDictionary *dic1 = @{@"name":@"Harry Potter",@"pages":@(256),@"author":@{@"name":@"J.K.Rowling",@"birthday":@"1975-07-31"},@"authors":@[@{@"name":@"J.K.Rowling",@"birthday":@"1976-07-31"},@{@"name":@"J.K.Rowling2",@"birthday":@"1977-07-31"},@{@"name":@"J.K.Rowling3",@"birthday":@"1978-07-31"}],@"publish":@"BeijingUniversity"};YYBook *book1 = [YYBook yy_modelWithDictionary:dic1];[book1.authors enumerateObjectsUsingBlock:^(YYAuthor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {NSLog(@"author name is--->%@",obj.name);}];///>>>model->jsonNSString *jsonStr1 = [book1 yy_modelToJSONString];NSLog(@"jsonStr1--->%@",jsonStr1);///>>>model->NSDictionaryNSDictionary *dic2 = [book1 yy_modelToJSONObject];NSLog(@"dic2--->%@",dic2);NSData *data = [book1 yy_modelToJSONData];NSLog(@"data str --->%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}@end

[iOS]YYModel用法总结相关推荐

  1. iOS YYModel的使用

    YYModel是YYKit的高效组件之一,在实际场景中的非常实用,运用于项目中使用MVC或MVVM架构时,使用model做数据处理. 一.常用的方法 // 字典转模型 + (nullable inst ...

  2. IOS YYModel

    文章目录 YYModel介绍 YYModel的安装 YYModel中的常用方法 举例 准备工作 代码 YYModel介绍 YYModel是iOS/OSX平台下一个高性能的模型转换框架,和同类的其他框架 ...

  3. IOS UIWebView用法

    转自猫猫小屋 IOS webview控件使用简介(一) IOS webview控件使用简介(二)–加载本地html 转载于:https://www.cnblogs.com/lairui1232000/ ...

  4. ios CGRec用法

       /*      rect(x,y,width,height);      width, height正负代表了从原点的绘制方向,矩形的长宽都是取得绝对值      */              ...

  5. iOS YYModel使用方法

    文档地址:https://github.com/ibireme/YYModel NSDictionary *testDic = @{@"groupId":@"01&quo ...

  6. iOS storyboard 用法详解

    发布于:2013-12-13 15:57阅读数:118220 Storyboard是一项令人兴奋的功能,在iOS5中首次推出,在开发app的界面时可以极大地节省时间. 如下图所示,这就是一个完整的应用 ...

  7. ios yymodel 将字典转数组模型_Python3 字典

    Python3 字典 Python AI开发实战营 - 一堂课快速认识Python机器学习 - 创客学院直播室​www.makeru.com.cnPython AI开发实战营 - Day1:建立pyt ...

  8. ios NSFileManager 用法详解

    2019独角兽企业重金招聘Python工程师标准>>> iPhone文件系统NSFileManager讲解是本文要介绍的内容,主要是通过iphone文件系统来学习NSFileMana ...

  9. ios RunLoop 用法

    2019独角兽企业重金招聘Python工程师标准>>> A run loop for a given thread will wait until one or more of it ...

最新文章

  1. keras回调监控函数
  2. 365个机器学习概念,“耐撕”的AI日历限量预售 | 新年礼物嗷~
  3. scikit-learn的主要模块和基本使用
  4. c# 获取excel单元格公式结果_每日一Excel技巧(熟能生巧):带公式的单元格,快速批量复制、填充...
  5. react学习(49)--参数判定
  6. Jmeter使用SSL(HTTPS协议)
  7. Windows下用C语言连接Mysql注意问题
  8. Layui组件 sliderVerify 实现滑块验证
  9. 离散时间信号处理第三版英文版课后习题答案
  10. Window系统安装FFmpeg教程
  11. Caused by: javax.security.auth.login.LoginException: unable to find LoginModule class: com.ibm.secur
  12. roadrunner中文文档(四)app服务器
  13. IDEA使用Statistic插件统计代码数量
  14. java中级程序员面试
  15. java阴阳师抽卡概率_《阴阳师》手游随机抽取类玩法概率公示
  16. 网络服务器未运行是什么原因是,Win7系统网络诊断提示诊断策略服务未运行怎么办?...
  17. OTL电路与OCL功放电路的区别
  18. VC++ 利用MFC的CWindowDC类实现画线功能 在桌面窗口中画线 绘制彩色线条 CPen nPenStyle nWidth crColor
  19. Mac OSX: 有线共享WiFi
  20. python读取txt各个数字

热门文章

  1. java rpc motan_RPC框架motan使用
  2. 数据挖掘工程师的基本职责。
  3. Android安卓的家教平台设计小程序app毕业设计
  4. CUDA版本11.4,pytorch应该下载哪个版本的
  5. 关于微信分享logo图片不显示,带有微信敏感文字
  6. 【MySQL】数据库相关操作思维导图
  7. 在vSphere上通过BOSH工具大规模部署Cloud Foundry (1) - IaaS准备
  8. 引用折叠 万能引用 模板参数推导
  9. 白帽子漏洞提交众测平台(网站链接)
  10. SAP HANA学习