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;
 
 

 

posted on 2016-06-01 11:45  Evan*少  阅读(929)  评论(0编辑  收藏  举报

导航