original Preface Before you begin, it is recommended to read the basic understanding of iOS runtime: iOS internal skills: runtime During an interview, a guy encountered such a question: "Do you know the nature of member variables?" The guy was immediately confused. What is the nature of member variables? Member variables are member variables. They are just used in daily life. What is the deeper meaning? This article focuses on the definition and use of member variables and properties in runtime. Noun Analysis Member variables 1. Definition: Ivar: instance variable type, a pointer to the objc_ivar structure
2. Operation function:
3. Use cases: The Model header file is declared as follows:
Get its member variables:
Print results:
property 1. Definition: objc_property_t: The type of the declared property, a pointer to the objc_property structure
2. Operation function:
Note: Using class_copyPropertyList will not get member variables without @property declaration
illustrate: The property_getAttributes function returns a list of objc_property_attribute_t structures. The objc_property_attribute_t structure contains name and value. The commonly used attributes are as follows:
The description obtained using property_getAttributes is a general description of all the names and values that property_copyAttributeList can obtain, such as T@"NSDictionary",C,N,V_dict1 3. Use cases:
Print results:
Application Examples 1. Conversion from Json to Model In development, I believe the most common thing is that interface data needs to be converted into Model (of course, if you take values directly from Dict...), many developers also use well-known third-party libraries such as JsonModel, Mantle or MJExtension, etc. If you only use them without knowing why, it is really no different from "moving bricks". Next, we use runtime to parse json to assign values to the Model. Principle description: Use the function provided by runtime to traverse all attributes of the Model itself. If the attribute has a corresponding value in json, assign it. Core method: Add method to NSObject category:
Readers can further think about:
Try to solve the above problems, and you can also write your own full-featured Json to Model library. 2. Quick Archiving Sometimes we need to archive some information, such as the user information class UserInfo, which will require overriding the initWithCoder and encodeWithCoder methods, and performing encode and decode operations on each attribute. Then the question is: when there are only a few attributes, it can be easily written, but what if there are dozens of attributes? Wouldn't it take forever to write? . . . Principle description: Use the function provided by runtime to traverse all attributes of the Model itself, and encode and decode the attributes. Core method: Override the method in the base class of Model:
3. Accessing private variables We know that there are no real private variables and methods in OC. To make member variables private, they must be declared in the m file and not exposed to the outside world. If we know the name of the member variable, we can get the member variable through runtime and then get its value through getIvar. method:
Everyone is welcome to communicate and discuss. |
<<: Let's take a look at iOS local persistent storage (focusing on the SQLite database)
>>: Comprehensive analysis of Activity startup mode
With the arrival of the 618 mid-year sale, educat...
Some media have conducted evaluations and found th...
Recently, the China Academy of Information and Co...
Every summer, when the flood season arrives, cont...
When you think of injections, do you think of &qu...
With the continuous popularity of short videos, ma...
[Girls' Emotions] Love Psychology to Save Sin...
In writing this article, I would like to clarify ...
Author: Li Chuanfu Shi Xiangqi The Taklimakan Des...
Compiled by: Gong Zixin For decades, the high pre...
The homepage of a website is extremely important ...
According to foreign media reports, the reason wh...
Traffic costs are becoming more and more expensiv...
In recent years, I have seen many user operations...
1. Platform Overview "Ali App Distribution&q...