YYModel Summary
生活随笔
收集整理的這篇文章主要介紹了
YYModel Summary
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
YYModel Effect-> YYModel的作用 Provide some data-model method—>提供一些數(shù)據(jù)模型的方法 Convert json to any object, or convert any object to json.->對任何對象轉(zhuǎn)換成JSON,和對任何JSON轉(zhuǎn)換為對象 Set object properties with a key-value dictionary (like KVC).設(shè)置一個屬性與鍵值對字典(如KVC) KVC -> key - value - coding(鍵值編碼) Implementations of `NSCoding`, `NSCopying`, `-hash` and `-isEqual:`.->對鍵值編碼、拷貝、哈希、和一樣的 See `YYModel` protocol for custom methods.看到Y(jié)YModel自定義的方法 Sample Code -> 舉例子 ********************** json convertor ********************* ?JSON轉(zhuǎn)模型對象 @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; 書本頁數(shù)? @property (nonatomic, strong) YYAuthor *author; 書本的作者 @end
???? @implementation YYBook
???? @end
??? int main()?{ // create model from json -> 從JSON字符串創(chuàng)建模型 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]; 從模型轉(zhuǎn)JSON字符串 // {"author":{"name":"J.K.Rowling","birthday":"1965-07-31T00:00:00+0000"},"name":"Harry Potter","pages":256} }
??? 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) {// 自定義類字典 ->返回類創(chuàng)建從這字典,使用這個類 cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
??? }
???
??? NSObject *one = [cls new];
??? if ([one yy_modelSetWithDictionary:dictionary]) return one;
??? return nil; } // 這里是把JSON轉(zhuǎn)化為字典的實現(xiàn)方法 + (NSDictionary *)_yy_dictionaryWithJSON:(id)json {// 字典WithJSON if (!json || json == (id)kCFNull) return nil;// 如果JSON為空直接return NSDictionary *dic = nil;// 創(chuàng)建一個空的字典 NSData *jsonData = nil;// 創(chuàng)建一個空的數(shù)據(jù) // ?因為就只有三種格式可以轉(zhuǎn)換為字典模型的(JSON、字符串、數(shù)據(jù)) if ([json isKindOfClass:[NSDictionary class]]) { dic = json; } else if ([json isKindOfClass:[NSString class]]) {// 如果數(shù)據(jù)類型為字符串的話,還要進行一個NSData轉(zhuǎn)換 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, ->如果你需要創(chuàng)建實例類在JSON->對象變換 use the method to choose custom class based on dictionary data.-> 利用字典數(shù)據(jù)選擇自定義類的方法 @discussion If the model implements this method, ->如果這個模型實現(xiàn)咯這個方法 it will be called to determine resulting class -> 它將被調(diào)用產(chǎn)生的類 during `+modelWithJSON:`, `+modelWithDictionary:`, ->在轉(zhuǎn)換的期間里JSON轉(zhuǎn)換模型或自定啊轉(zhuǎn)換模型 conveting object of properties of parent objects -> 轉(zhuǎn)換對父對象的屬性對象 (both singular and containers via `+modelContainerPropertyGenericClass`). ->容器通過
?Example:例子
??????? @class YYCircle, YYRectangle, YYLine;
?
??????? @implementation YYShape // 這樣判斷更為嚴(yán)謹(jǐn) + (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. ->返回類創(chuàng)建從這字典,使用這個類 */ cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
???? @property (nonatomic, assign) NSDate *birthday; 生日
???? @end
???? @implementation YYAuthor ??
???? @end @interface YYBook : NSObject ?-> 書本類 @property (nonatomic, copy) NSString *name; ?書本名字 @property (nonatomic, assign) NSUInteger pages; 書本頁數(shù)? @property (nonatomic, strong) YYAuthor *author; 書本的作者 @end
???? @implementation YYBook
???? @end
??? int main()?{ // create model from json -> 從JSON字符串創(chuàng)建模型 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]; 從模型轉(zhuǎn)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.創(chuàng)建和返回一個JSON從接收器中的一個新實例 This method is thread-safe. 這個方法是線程安全的 @param json? A json object in `NSDictionary`, `NSString` or `NSData`.字典參數(shù)(JSON對象、字符串、和數(shù)據(jù)) @return A new instance created from the json, or nil if an error occurs.返回一個新的JSON格式的實例對象或者如果出現(xiàn)錯誤零 */ + (nullable instancetype)yy_modelWithJSON:(id)json; ?外界傳一個JSON給我我返回一個模型給他 實現(xiàn): + (instancetype)yy_modelWithJSON:(id)json { NSDictionary *dic = [self _yy_dictionaryWithJSON:json];// 這里就把JSON轉(zhuǎn)化為字典 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) {// 自定義類字典 ->返回類創(chuàng)建從這字典,使用這個類 cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
??? }
???
??? NSObject *one = [cls new];
??? if ([one yy_modelSetWithDictionary:dictionary]) return one;
??? return nil; } // 這里是把JSON轉(zhuǎn)化為字典的實現(xiàn)方法 + (NSDictionary *)_yy_dictionaryWithJSON:(id)json {// 字典WithJSON if (!json || json == (id)kCFNull) return nil;// 如果JSON為空直接return NSDictionary *dic = nil;// 創(chuàng)建一個空的字典 NSData *jsonData = nil;// 創(chuàng)建一個空的數(shù)據(jù) // ?因為就只有三種格式可以轉(zhuǎn)換為字典模型的(JSON、字符串、數(shù)據(jù)) if ([json isKindOfClass:[NSDictionary class]]) { dic = json; } else if ([json isKindOfClass:[NSString class]]) {// 如果數(shù)據(jù)類型為字符串的話,還要進行一個NSData轉(zhuǎn)換 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, ->如果你需要創(chuàng)建實例類在JSON->對象變換 use the method to choose custom class based on dictionary data.-> 利用字典數(shù)據(jù)選擇自定義類的方法 @discussion If the model implements this method, ->如果這個模型實現(xiàn)咯這個方法 it will be called to determine resulting class -> 它將被調(diào)用產(chǎn)生的類 during `+modelWithJSON:`, `+modelWithDictionary:`, ->在轉(zhuǎn)換的期間里JSON轉(zhuǎn)換模型或自定啊轉(zhuǎn)換模型 conveting object of properties of parent objects -> 轉(zhuǎn)換對父對象的屬性對象 (both singular and containers via `+modelContainerPropertyGenericClass`). ->容器通過
?Example:例子
??????? @class YYCircle, YYRectangle, YYLine;
?
??????? @implementation YYShape // 這樣判斷更為嚴(yán)謹(jǐn) + (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. ->返回類創(chuàng)建從這字典,使用這個類 */ cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;
?
轉(zhuǎn)載于:https://www.cnblogs.com/happyEveryData/p/5549093.html
總結(jié)
以上是生活随笔為你收集整理的YYModel Summary的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mycat 安装配置
- 下一篇: 构造函数的反射