美文网首页
YYModel源码阅读(1)

YYModel源码阅读(1)

作者: 程序狗 | 来源:发表于2017-04-25 13:57 被阅读22次

最近项目进入空闲期,把YYModel主要的方法重新打了一遍,希望能够帮助自己理解。写下此文,也是做个记录。

首先看第一个方法,我们从YYModelExample.m文件里面可以看到


static void SimpleObjectExample() {

YYBook *book = [YYBook modelWithJSON:@"    \

{                                          \

\"name\": \"Harry Potter\",              \

\"pages\": 512,                          \

\"publishDate\": \"2010-01-01\"          \

}"];

NSString *bookJSON = [book modelToJSONString];

NSLog(@"Book: %@", bookJSON);

}

modelWithJSON:把JSON字符串映射到对应的Model里面

我们点进去看看接下来走的方法.


+ (instancetype)modelWithJSON:(id)json {

NSDictionary*dic = [self _yy_dictionaryWithJSON:json];

return[self modelWithDictionary:dic];

}

其中_yy_dictionaryWithJSON是通过[NSJSONSerialization JSONObjectWithData:option:error]把JSON对象转换NSDictionary对象,接下来重头戏来了,modelWithDictionary:


+ (instancetype)modelWithDictionary:(NSDictionary*)dictionary {

if(!dictionary || dictionary == (id)kCFNull) return nil;

if(![dictionaryisKindOfClass:[NSDictionaryclass]]) return nil;

Classcls = [self class];

_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];

NSLog(@"modelMeta : %@",modelMeta);

if (modelMeta->_hasCustomClassFromDictionary) {

cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;

}

NSObject*one = [cls new];

if ([one modelSetWithDictionary:dictionary]) return one;

return nil;

}

其中YYModel设计一个这样的YYmodelMeta 这样的来获取当前类的一些信息。


@interface_YYModelMeta :NSObject{

@package

YYClassInfo*_classInfo;

NSDictionary*_mapper;// json key 和 property Meta 的映射关系字典

NSArray*_allPropertyMetas;// 所有属性的propertyMeta

NSArray*_keyPathPropertyMetas;// 映射jsonkeyPath 的PropertyMetas

NSArray*_multiKeysPropertyMetas;// 映射多个jsonKey的propertyMeta

NSUInteger_keyMappedCount;//需要映射的属性的总数

YYEncodingNSType_nsType;/// Model对应的Foundation class类型

//是否实现了自定义的映射关系

BOOL_hasCustomWillTransformFromDictionary;

BOOL_hasCustomTransformFromDictionary;

BOOL_hasCustomTransformToDictionary;

BOOL_hasCustomClassFromDictionary;

}

其中YYClassInfo其实是一个复合类,其中里面包含了类的成员变量ivar,类的方法method,类的属性property。作者把三个分别封装成类,更好的被外部捕捉,提高运行效率。

我们接下来先解析这个类YYClassInfo

相关文章

  • YYModel源码学习

    YYModel源码阅读 1.Demo简要介绍: 只有2个实现文件,NSObject+YYModel 和 YYCla...

  • IOS笔记:撸一个 JSON->Model 之 YYMo

    YYModel 源码阅读笔记:首先调用 1. + (instancetype)modelWithJSON:(id)...

  • YYModel源码详细解析-2

    前言 阅读之前请见阅读YYModel源码详细解析-1,废话不多说,继续解析源码。 _YYModelMeta类 _Y...

  • iOS源码阅读系列

    iOS源码阅读系列 AFNetworking SDWebimage YYModel

  • YYModel源码阅读(1)

    最近项目进入空闲期,把YYModel主要的方法重新打了一遍,希望能够帮助自己理解。写下此文,也是做个记录。 首先看...

  • 转:YYModel源码详细解析-1

    YYModel源码详细解析-1 js丶关注 2016.06.18 01:59*字数 3167阅读 3800评论 1...

  • YYImage/YYModel/YYCache

    1.YYImage源码分析2.YYModel源码分析3.郑钦洪_:YYModel 源码历险记model属性赋值过程...

  • YYModel源码阅读(2)

    我们来分析YYClassInfo这个类,一开始,作者写了3种编码 这里分别有3个Mask,分别是YYEncodin...

  • iOS源码阅读 —— YYModel

    YYModel作为一个 iOS/OSX 模型转换框架,为JSON与数据模型之间的转换,提供了高性能的解决方案。 在...

  • YYKit

    揭秘 YYModel 的魔法(上)揭秘 YYModel 的魔法(下)YYCache 设计思路YYCache 源码解...

网友评论

      本文标题:YYModel源码阅读(1)

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