当前位置:网站首页>Internal implementation principle of yymodel
Internal implementation principle of yymodel
2022-07-28 05:06:00 【Lu_ Ca】
YYModel As a model transformation framework in the development process , by JSON And data model , Provides a high-performance solution .
I've been listening to others before ,YYModel The internal implementation is KVC Attribute assignment . It is also considered reasonable , But today, I found the source code , In fact, it is not used setValue:forKey: The way of assignment . Instead, it adopted objc_msgSend Method called the setter Method . in the future , Tell people YYModel Don't say yes KVC 了 , At first glance, I haven't seen the source code . Next stick a sentence assignment source
case YYEncodingTypeNSMutableString: {
if ([value isKindOfClass:[NSString class]]) {
if (meta->_nsType == YYEncodingTypeNSString) {
((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);
} else {
((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, ((NSString *)value).mutableCopy);
}
} It's clearly written inside ((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);
Next, I'll write down the general process of the transformation between the model and the data I see .
Json turn Model
We usually call methods yy_modelWithJson Perform the conversion operation . In fact, this function mainly implements the next four steps :
1、 First of all json Serialize to an available dictionary , And then call yy_modelWithDictionary Method
2、yy_modelWithDictionary Yes Json Type determination , Create instances of objects through class objects , And pass yy_modelSetWithDictionary Assign a value
3、 stay yy_modelSetWithDictionary In the method , adopt modelSetValueForProperty Method assignment .
4、modelSetValueForProperty To judge the attribute data type , And then sent through the invoked message objc_msgSend The way , Call again setter Method .
Model turn Json
Usually we call yy_modelToJSONString Method to convert . The implementation process of this function is roughly like this :
1、 By calling yy_modelToJSONData Back to data, And then through [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] Generated json character string
2、yy_modelToJSONData Method call yy_modelToJSONObject Method returns a id Object of type , And then through [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:NULL] Generate what you need in the first step NSData.
3、yy_modelToJSONObject Call in ModelToJSONObjectRecursive Method .
4、 then ModelToJSONObjectRecursive It mainly preprocesses objects , To become effective json object .
ModelToJSONObjectRecursive Method process
- ModelToJSONObjectRecursive Method for direct transfer json Between the attributes of .
- For attributes of collection classes, such as dictionaries 、 Array, etc. through recursive calls ModelToJsonObjectRecursive Method to constantly check whether each element can be directly transferred json. If you can directly return the collection class , If not , Create a new collection , Traverse every element before , Turn the elements inside into NSString、NSNumber, Then add it to the collection class after parsing .
- Custom class object , Use NSDictionary Object to reassemble , adopt objc_msgSend call getter The method value of , Then turn into effective one by one json object .
️: The above is my personal conclusion by checking the source code , If there are mistakes, please correct them , Mutual encouragement
边栏推荐
- (clone virtual machine steps)
- Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
- Know etcd
- C语言ATM自动取款机系统项目的设计与开发
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
- Anaconda common instructions
- flink思维导图
- Service object creation and use
- 【ARXIV2203】SepViT: Separable Vision Transformer
- Melt cloud x chat, create a "stress free social" habitat with sound
猜你喜欢

Dynamic SQL and paging
![(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection

Comprehensively analyze the differences between steam and maker Education

C语言ATM自动取款机系统项目的设计与开发

Online sql to XML tool

Microservice failure mode and building elastic system

flink思维导图

FreeRTOS startup process, coding style and debugging method

为什么md5不可逆,却还可能被md5免费解密网站解密

Table image extraction based on traditional intersection method and Tesseract OCR
随机推荐
[daily question 1] 735. Planetary collision
Angr(十一)——官方文档(Part2)
Interview fraud: there are companies that make money from interviews
Analyze the emotional elements contained in intelligent sweeping robot
App test process and test points
Easycvr Video Square snapshot adding device channel offline reason display
[learning record] data enhancement 1
Redis配置文件详解/参数详解及淘汰策略
HDU 3078 network (lca+ sort)
How to successfully test php7.1 connecting to sqlserver2008r2
Angr (XI) - official document (Part2)
Have you learned the common SQL interview questions on the short video platform?
这种动态规划你见过吗——状态机动态规划之股票问题(中)
HDU 3592 World Exhibition (differential constraint)
从微服务基本概念到核心组件-通过一个实例来讲解和分析
【内功心法】——函数栈帧的创建和销毁(C实现)
Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
Special topic of APP performance design and Optimization - poor implementation affecting performance
How does Alibaba use DDD to split microservices?
What is the core value of testing?