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;

?

轉載于:https://www.cnblogs.com/happyEveryData/p/5549093.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/374594.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374594.shtml
英文地址,請注明出處:http://en.pswp.cn/news/374594.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

iOS學習——ScrollView圖片輪播和同類控件優先級問題

iOS學習——ScrollView的使用和同類控件優先級問題 1. 布置界面 ScrollView的使用非常簡單,只有三步 1.1 添加一個scrollview 1.2 向scrollview添加內容 1.3 告訴scrollview中內容的實際大小 首先做第一步,布置界面。 拖拽一個scrollview就可以了 就…

Exchanger和無GC的Java

總覽 Exchanger類在線程之間傳遞工作和回收使用的對象方面非常有效。 AFAIK,它也是最少使用的并發類之一。 但是,如果您不需要GC,則使用ArrayBlockingQueue進行日志記錄會更簡單。 交換器類 Exchanger類對于在兩個線程之間來回傳遞數據很有…

構造函數的反射

1 import java.lang.reflect.Constructor;2 3 public class zzbds {4 public static void main(String[] args) {5 6 try{ 7 Class cStudent.class; //獲得無參構造函數8 Constructor constructorc.getConstructor(new Class[]{…

字符串連接“+”int、char、string

String s1 "21" "8" "54";System.out.println(s1);String s2 "21" 8 "54";System.out.println(s2);String s3 "21" 8 "54";System.out.println(s3);21854 21854 21854

使用Spring使用Java發送電子郵件– GMail SMTP服務器示例

對于使用Java發送電子郵件, JavaMail API是標準解決方案。 如官方網頁所述,“ JavaMail API提供了獨立于平臺和協議的框架來構建郵件和消息傳遞應用程序”。 必需的類包含在JavaEE平臺中,但是要在獨立的JavaSE應用程序中使用它,您…

Java字符與數字的計算

先看例子: char ch;int x;int y 7;System.out.print("7的ASCII碼值是:");System.out.println(y);ch 7 2;System.out.print("7 2的char型:");System.out.println(ch);x 7 2;System.out.print("7 2的int型&…

wordcount

源代碼如下 package org.apache.hadoop.examples; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io…

EJB 3.1全局JNDI訪問

如本系列前面的部分所述,EJB 3.0版規范的主要缺點是缺少可移植的全局JNDI名稱。 這意味著沒有可移植的方式將EJB引用鏈接到應用程序外部的Bean。 EJB v。3.1規范用自己的話填補了這一定義: “一個標準化的全局JNDI名稱空間和一系列相關的名稱空間&#…

Git 分支管理和沖突解決

創建分支 git branch 沒有參數,顯示本地版本庫中所有的本地分支名稱。 當前檢出分支的前面會有星號。 git branch newname 在當前檢出分支上新建分支,名叫newname。 git checkout newname 檢出分支,即切換到名叫newname的分支。 git checkout…

力扣打開轉盤鎖

打開轉盤鎖 評論區大神代碼&#xff1a; public int openLock(String[] deadends, String target) {Set<String> set new HashSet<>(Arrays.asList(deadends));//開始遍歷的字符串是"0000"&#xff0c;相當于根節點String startStr "0000";i…

EJB程序化查找

在上一篇文章中&#xff0c;我們了解了EJB 引用和EJB 注入 。 盡管EJB注入是一種強大的容器工具&#xff0c;可以簡化模塊化應用程序的開發&#xff0c;但有時還是需要執行程序化EJB查找。 讓我們假設&#xff0c;例如&#xff0c;一組不同的EJB實現了由公共業務接口定義的公共…

git克隆/更新/提交代碼步驟及示意圖

1. git clone ssh://flycm.intel.com/scm/at/atSrc 或者git clone ssh://flycm.intel.com/scm/at/atJar 或者git clone ssh://flycm.intel.com/scm/at/atFramework 2. git checkout cpeg/scm/stable 切換分支&#xff0c;然后更新代碼 3. git pull 先把遠程分支上最新的代碼拉到…

C++面試寶典

1.new、delete、malloc、free關系 delete會調用對象的析構函數,和new對應free只會釋放內存&#xff0c;new調用構造函數。malloc與free是C/C語言的標準庫函數&#xff0c;new/delete是C的運算符。它們都可用于申請動態內存和釋放內存。對于非內部數據類型的對象而言&#xff0c…

Google App Engine:在您自己的域中托管應用程序

在Google App Engine中創建新應用程序時&#xff0c;您將獲得一個域名“ yourapp.appspot.com”。 但是&#xff0c;誰會想要以這樣的后綴托管他們的應用程序&#xff08;除非您喜歡它&#xff01;&#xff09;&#xff1f; 為了改善您的應用品牌&#xff0c;最好的辦法是將您的…

從零開始學 iOS 開發的15條建議

事情困難是事實&#xff0c;再困難的事還是要每天努力去做是更大的事實。 因為我是一路自學過來的&#xff0c;并且公認沒什么天賦的前提下&#xff0c;進步得不算太慢&#xff0c;所以有很多打算從零開始的朋友會問我&#xff0c;該怎么學iOS開發。跟粉絲群的朋友交流了一下&a…

垂直居中-父元素高度確定的多行文本(方法二)

除了上一節講到的插入table標簽&#xff0c;可以使父元素高度確定的多行文本垂直居中之外&#xff0c;本節介紹另外一種實現這種效果的方法。但這種方法兼容性比較差&#xff0c;只是提供大家學習參考。 在 chrome、firefox 及 IE8 以上的瀏覽器下可以設置塊級元素的 display 為…

13. 羅馬數字轉整數

羅馬數字轉整數 class Solution {public int romanToInt(String s) {Map<Character,Integer> map new HashMap<Character,Integer>(){{put(I,1);put(V,5);put(X,10);put(L,50);put(C,100);put(D,500);put(M,1000);}};int res 0;for(int i 0;i<s.length();i)…

互聯網金融P2P主業務場景自動化測試

互聯網金融P2P行業&#xff0c;近三年來發展迅速&#xff0c;如火如荼。據不完全統計&#xff0c;全國有3000的企業。“互聯網”企業&#xff0c;幾乎每天都會碰到一些奇奇怪怪的bug&#xff0c;作為在互聯網企業工作的測試人員&#xff0c;風險和壓力都巨大。那么我們如何降低…

OSGi將Maven與Equinox結合使用

很長時間以來&#xff0c;我一直在努力理解OSGi的真正含義。 它已經存在很長時間了&#xff0c;但是沒有多少人意識到這一點。 人們已經大肆宣傳它是一種非常復雜的技術。 這是我為所有Java開發人員簡化的嘗試。 簡而言之&#xff0c; OSGi是一組規范&#xff0c;這些規范允許對…

note05-計算機網絡

5.網絡安全 被動攻擊(UDP報文被截獲 被 進行流量分析) 主動攻擊 1.篡改(更改報文流 偽報文) 2.惡意程序(病毒、木馬、蠕蟲、炸彈) 3.拒絕服務Dos 密碼體制 1.對稱密鑰密碼體制(DES IDEA) 即加密和解密的密鑰K相同 2.公鑰密碼體制(RSA) A加密使用PKB公鑰 B解密使用對應的私鑰SK…