YYModel Summary

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;

时间: 2025-01-06 17:50:17

YYModel Summary的相关文章

【HTML5】summary交互元素

1.源码 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"/> <title>交互元素summary的使用</title> <style type="text/css"> body{ padding:5px; font-size:14px; } summary{ font-weight:bold; } </style>

HDU 4989 Summary(数学题暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 Problem Description Small W is playing a summary game. Firstly, He takes N numbers. Secondly he takes out every pair of them and add this two numbers, thus he can get N*(N - 1)/2 new numbers. Thirdl

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 这个题目不难,首先对原序列进行排序,然后要注意对特殊情况的处理.同时,要掌握string类的几个重要成员函数: to_string():将数字转换为

Learning to Compare Image Patches via Convolutional Neural Networks --- Reading Summary

Learning to Compare Image Patches via Convolutional Neural Networks ---  Reading Summary 2017.03.08 Target: this paper attempt to learn a geneal similarity function for comparing image patches from image data directly. There are several ways in which

三个不常用的HTML元素:&lt;details&gt;、&lt;summary&gt;、&lt;dialog&gt;

前面的话 HTML5不仅新增了语义型区块级元素及表单类元素,也新增了一些其他的功能性元素,这些元素由于浏览器支持等各种原因,并没有被广泛使用 文档描述 <details>主要用于描述文档或文档某个部分的细节,与<summary>配合使用可以为<details>定义标题.标题是可见的,用户点击标题时,显示出details [注意]这两个标签只有chrome和opera支持 <details> 该标签仅有一个open属性,用来定义details是否可见(默认为不

[LeetCode]228.Summary Ranges

题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 代码 /*--------------------------------------- * 日期:2015-08-04 * 作者:SJF01

Java for LeetCode 228 Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 解题思路: JAVA实现如下: public List<String> summaryRanges(int[] nums) { List

Summary Ranges —— LeetCode

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 题目大意:给一个有序数组,返回连续的区间范围. public class Solution { public List<String>

Summary: Binary Search

Iterative ways: 1 int binarySearch (int[] a, int x) { 2 int low = 0; 3 int high = a.length - 1; 4 int mid; 5 6 while (low <= high) { 7 mid = (low + high) / 2; 8 if (a[mid] < x) { 9 low = mid + 1; 10 } 11 else if (a[mid] > x) { 12 high = mid - 1;