当前位置:网站首页>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
边栏推荐
- From the basic concept of micro services to core components - explain and analyze through an example
- 基于MPLS构建虚拟专网的配置实验
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- Pipe /createpipe
- How to send and receive reports through outlook in FastReport VCL?
- How to quickly turn function test to automatic test
- Redis type
- Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
- The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
- Comprehensively analyze the differences between steam and maker Education
猜你喜欢

Research on the design of robot education in stem course

Array or object, date operation

HashSet add

【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers

Observable time series data downsampling practice in Prometheus

Flink mind map

Transformer -- Analysis and application of attention model

MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?

How to quickly locate bugs? How to write test cases?

Data security is gradually implemented, and we must pay close attention to the source of leakage
随机推荐
[high CPU consumption] software_ reporter_ tool.exe
flink思维导图
Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
Online sql to XML tool
Supervisor series: 5. Log
Euler road / Euler circuit
What should testers know about login security?
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
【CVPR2022】On the Integration of Self-Attention and Convolution
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
go-zero单体服务使用泛型简化注册Handler路由
How to quickly turn function test to automatic test
微服务故障模式与构建弹性系统
FPGA: use PWM wave to control LED brightness
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
Pipe /createpipe
[daily one] visual studio2015 installation in ancient times
Analyze the emotional elements contained in intelligent sweeping robot
Flink mind map
What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?