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.mzph.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…