当前位置:网站首页>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
边栏推荐
- Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
- Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
- Array or object, date operation
- POJ 3728 the merchant (online query + double LCA)
- Know etcd
- Handling of web page image loading errors
- Data imbalance: comprehensive sampling of anti fraud model (data imbalance)
- Summary and review of puppeter
- HDU 3585 maximum shortest distance
- 【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
猜你喜欢

Melt cloud x chat, create a "stress free social" habitat with sound

Reading notes of SMT practical guide 1

Design and development of C language ATM system project

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

Know etcd

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight

【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation

What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?

MySQL(5)

Redis type
随机推荐
数据安全逐步落地,必须紧盯泄露源头
Testcafe's positioning, operation of page elements, and verification of execution results
Summary and review of puppeter
Euler road / Euler circuit
The research group passed the thesis defense successfully
Applet import project
Array or object, date operation
面试了一位38岁程序员,听说要加班就拒绝了
POJ 3728 the merchant (online query + double LCA)
Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
MySQL(5)
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
[high CPU consumption] software_ reporter_ tool.exe
Have you learned the common SQL interview questions on the short video platform?
Leetcode 15. sum of three numbers
Research on the design of robot education in stem course
Win10 machine learning environment construction pycharm, anaconda, pytorch
Online sql to XML tool
When initializing with pyqt5, super() and_ init _ () problems faced by the coordinated use of functions, as well as the corresponding learning and solutions
[function document] torch Histc and paddle Histogram and numpy.histogram