YYModel Effect-> YYModel的作用
Provide some data-model method—>提供一些数据模型的方法
Convert json to any object, or convert any object to json.->对任何对象转换成JSON,和对任何JSON转换为对象
Set object properties with a key-value dictionary (like KVC).设置一个属性与键值对字典(如KVC)
KVC -> key - value - coding(键值编码)
Implementations of `NSCoding`, `NSCopying`, `-hash` and `-isEqual:`.->对键值编码、拷贝、哈希、和一样的
See `YYModel` protocol for custom methods.看到YYModel自定义的方法
Sample Code -> 举例子
********************** json convertor *********************  JSON转模型对象
@interface YYAuthor : NSObject -> 作者类
@property (nonatomic, strong) NSString *name;  名字
     @property (nonatomic, assign) NSDate *birthday; 生日
     @end
     @implementation YYAuthor   
     @end
@interface YYBook : NSObject  -> 书本类
@property (nonatomic, copy) NSString *name;  书本名字
@property (nonatomic, assign) NSUInteger pages; 书本页数 
@property (nonatomic, strong) YYAuthor *author; 书本的作者
@end
     @implementation YYBook
     @end
   
int main() {
// create model from json -> 从JSON字符串创建模型
YYAuthor *author = [YYAuthor yy_modelWithJSON:@“{\”name\”:\”Jack\”},\“brithday\”:\”1994-10-22\"}”];
YYBook *book = [YYBook yy_modelWithJSON:@"{\"name\": \"Harry Potter\", \"pages\": 256, \"author\": {\"name\": \"J.K.Rowling\", \"birthday\": \"1965-07-31\" }}"];
 
// convert model to json
NSString *json = [book yy_modelToJSONString]; 从模型转JSON字符串
// {"author":{"name":"J.K.Rowling","birthday":"1965-07-31T00:00:00+0000"},"name":"Harry Potter","pages":256}
}

frist method

+ (nullable instancetype)yy_modelWithJSON:(id)json;  外界传一个JSON给我我返回一个模型给他

YYModel的方法
/**
Creates and returns a new instance of the receiver from a json.创建和返回一个JSON从接收器中的一个新实例
This method is thread-safe. 这个方法是线程安全的
@param json  A json object in `NSDictionary`, `NSString` or `NSData`.字典参数(JSON对象、字符串、和数据)
@return A new instance created from the json, or nil if an error occurs.返回一个新的JSON格式的实例对象或者如果出现错误零
*/
+ (nullable instancetype)yy_modelWithJSON:(id)json;  外界传一个JSON给我我返回一个模型给他
实现:
+ (instancetype)yy_modelWithJSON:(id)json {
NSDictionary *dic = [self _yy_dictionaryWithJSON:json];// 这里就把JSON转化为字典
return [self yy_modelWithDictionary:dic];
}
// 外界传字典进来返回一个模型
+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary {
    if (!dictionary || dictionary == (id)kCFNull) return nil;
    if (![dictionary isKindOfClass:[NSDictionary class]]) return nil;
   
Class cls = [self class];
//  Returns the cached model class meta返回存储模型类元
_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];
if (modelMeta->_hasCustomClassFromDictionary) {// 自定义类字典
->返回类创建从这字典,使用这个类
cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
    }
   
    NSObject *one = [cls new];
    if ([one yy_modelSetWithDictionary:dictionary]) return one;
    return nil;
}
// 这里是把JSON转化为字典的实现方法
+ (NSDictionary *)_yy_dictionaryWithJSON:(id)json {// 字典WithJSON
if (!json || json == (id)kCFNull) return nil;// 如果JSON为空直接return
NSDictionary *dic = nil;// 创建一个空的字典
NSData *jsonData = nil;// 创建一个空的数据
//  因为就只有三种格式可以转换为字典模型的(JSON、字符串、数据)
if ([json isKindOfClass:[NSDictionary class]]) {
dic = json;
} else if ([json isKindOfClass:[NSString class]]) {// 如果数据类型为字符串的话,还要进行一个NSData转换
jsonData = [(NSString *)json dataUsingEncoding : NSUTF8StringEncoding];
    } else if ([json isKindOfClass:[NSData class]]) {
        jsonData = json;
    }
    if (jsonData) {
        dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
        if (![dic isKindOfClass:[NSDictionary class]]) dic = nil;
    }
    return dic;
}
/**
If you need to create instances of different classes during json->object transform, ->如果你需要创建实例类在JSON->对象变换
use the method to choose custom class based on dictionary data.-> 利用字典数据选择自定义类的方法
@discussion If the model implements this method, ->如果这个模型实现咯这个方法
it will be called to determine resulting class -> 它将被调用产生的类
during `+modelWithJSON:`, `+modelWithDictionary:`, ->在转换的期间里JSON转换模型或自定啊转换模型
conveting object of properties of parent objects -> 转换对父对象的属性对象
(both singular and containers via `+modelContainerPropertyGenericClass`). ->容器通过

 Example:例子
        @class YYCircle, YYRectangle, YYLine;
 
        @implementation YYShape
// 这样判断更为严谨
+ (Class)modelCustomClassForDictionary:(NSDictionary*)dictionary {
if (dictionary[@"radius"] != nil) {// 如果半径不为空
return [YYCircle class];// 返回一个圈
} else if (dictionary[@"width"] != nil) { // 如果宽度不为空
return [YYRectangle class]; // 返回一个矩形
} else if (dictionary[@"y2"] != nil) { // 如果Y值不为空
return [YYLine class]; // 那么返回一跳线
} else {
return [self class]; // 如果都不满足返回自己这个类
}
        }

@end

@param dictionary The json/kv dictionary.

@return Class to create from this dictionary, `nil` to use current class. ->返回类创建从这字典,使用这个类
*/
cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;

转载于:https://www.cnblogs.com/happyEveryData/p/5549093.html

YYModel Summary相关推荐

  1. tensorflow在训练和验证时监视不同的summary的操作

    如果想在训练和验证时监视不同的summary,将train summary ops和val summary ops放进不同的集合中即可. train_writer = tf.summary.FileW ...

  2. LeetCode 228: Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  3. R语言单变量分析实战:汇总统计(Summary Statistics)、频率表(Frequency Table)、图表(charts: boxplot、histogram、density)

    R语言单变量分析实战:汇总统计(Summary Statistics).频率表(Frequency Table).图表(charts: boxplot.histogram.density) 目录

  4. R语言使用R基础安装中的glm函数构建乳腺癌二分类预测逻辑回归模型、分类预测器(分类变量)被自动替换为一组虚拟编码变量、summary函数查看检查模型、使用table函数计算混淆矩阵评估分类模型性能

    R语言使用R基础安装中的glm函数构建乳腺癌二分类预测逻辑回归模型(Logistic regression).分类预测器(分类变量)被自动替换为一组虚拟编码变量.summary函数查看检查模型.使用t ...

  5. R语言manova函数进行多元方差分析(Multivariate analysis of variance 、MANOVA)、如果多变量检验结果有显著性,summary.aov函数锦欣单变量方差分析

    R语言使用manova函数进行多元方差分析(Multivariate analysis of variance (MANOVA)).如果多变量检验结果具有显著性,使用summary.aov函数获得单变 ...

  6. R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型的混淆矩阵、并使用summary函数基于混淆矩阵输出分类模型评估的其它详细指标(kappa、npv等13个)

    R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型的混淆矩阵(confusion matrix).并使用summary函数基于混淆矩阵输出分类模型评估的其它详细 ...

  7. R语言使用skimr包的skim函数查看整个dataframe数据集的summary信息、统计汇总信息(Summarize a whole dataset)

    R语言使用skimr包的skim函数查看整个dataframe数据集的summary信息.统计汇总信息(Summarize a whole dataset) 目录

  8. R语言使用lm构建线性回归模型、并将目标变量对数化实战:可视化模型预测输出与实际值对比图、可视化模型的残差、模型系数(coefficient)、模型总结信息(summary)、残差总结信息

    R语言使用lm构建线性回归模型.并将目标变量对数化(log10)实战:可视化模型预测输出与实际值对比图.可视化模型的残差.模型系数(coefficient).模型总结信息(summary).残差总结信 ...

  9. R语言进行主成分分析(PCA):使用prcomp函数来做主成分分析、使用summary函数查看主成分分析的结果、计算每个主成分解释方差的、每个主成分解释的方差的比例、以及多个主成分累积解释的方差比例

    R语言进行主成分分析(PCA):使用prcomp函数来做主成分分析.使用summary函数查看主成分分析的结果.计算每个主成分解释方差的.每个主成分解释的方差的比例.以及多个主成分累积解释的方差比例 ...

最新文章

  1. [html] Google map的地理编码javascript例子 -- Google map geocoder example
  2. CSS设置像文字一样的按钮
  3. c语言实战1200例 pdf6,C语言程序设计6.6.2.pdf
  4. jQuery的DOM操作之取值/赋值(1)
  5. 数据分析工具评测丨Yonghong Desktop对战Tableau Desktop
  6. 基于微服务架构的token生成和使用
  7. 14种模式解决面试算法编程题(PART I)
  8. 学习 shell —— 相对路径转换为绝对路径
  9. Raphael的transform用法
  10. Julia :元编程、宏
  11. Matlab语言标点说明
  12. linux运行海康,海康摄像头SDK在Linux、windows下的兼容问题(二)已解决
  13. vscode 折叠/展开所有区域代码快捷键
  14. 应届生拿到offer之后的流程_应届生雷区:拿到offer不想去了怎么办?小心登上HR黑名单!...
  15. 基于LSTM神经网络的负荷预测(Python代码实现)
  16. RVN 一种新的聚类算法
  17. Nginx反向代理服务器解决负责均衡问题
  18. 素材之家,中国免费素材下载网站!下免费素材就到素材之家!
  19. 10.高光反射Blinn
  20. Oracle 11g加密备份

热门文章

  1. MFC程序打开文件对话框出错的问题解决
  2. Linux常用命令和vi,gdb的使用
  3. 1000以内完数c语言程序_C语言经典面试题目及答案详解(二)
  4. 三种单例模式的C++实现
  5. 西安单招学校学计算机软件的,西北大学软件职业技术学院单招
  6. 参数php_PHP多参数方法的重构
  7. Flink应用实战案例50篇(五)-Apache Flink 在 bilibili 的多元化探索与实践
  8. 那些年职场老鸟都踩过哪些坑?送给后来人的一些职场建议
  9. python名称与作用域_Python变量命名与作用域的坑
  10. python可以用来编写计算机网络程序吗_计算机网络(基于python做的笔记 )