YYModel Summary
生活随笔
收集整理的這篇文章主要介紹了
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} }
??? 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;
???? @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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mycat 安装配置
- 下一篇: 构造函数的反射