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
Course List Lesson 1-The relationship between bac...
"The busiest time is yet to come." As t...
Zheng Xiangzhou-New Business Model Complete Works...
In previous articles, Qingguajun has always empha...
Tianzhou spacecraft publicly "collects items...
A company that started out as a video website eve...
This article mainly introduces what kind of posts...
Last month, the Camera and Imaging Products Associ...
[[164448]] One of the great benefits of the mobil...
Some time ago, a celebrity wore a mouth guard to ...
[Mobile software: Bo Ke Yuan] A new study publish...
What is the price for customizing the Sanya Trave...
The 5G world is surging in 2019. The communicatio...
In daily life and chatting, the word "嗯"...