Summary of basic principles of iOS
In the previous article on class loading (Part 1), you understood how classes are loaded into memory from Mach-O. Now let’s explain how classes are loaded into classes and how they work together
Nature of classification
Premise: Define the category LG for LGperson in main
There are three ways to explore the nature of categorization
- [Method 1] Through
clang
- [Method 2] Pass
Xcode
Document searchCategory
- [Method 3] Pass
objc
The source code searchcategory_t
Method 1: Clang
-
Clang-rewrite-objc LGPerson+ lga. m -o LGPerson+ lga. CPP
- Among them
classification
Is of type_category_t
- The penultimate 0 of the category is zero
No agreement
, soThe assignment is 0
- Among them
Search for struct _category_t as shown below
- There are two method_list_t, respectively
Instance methods
和Class method
Search for _CATEGORY_INSTANCE_METHODS_LGPerson_ to find its underlying implementation
There are five methods in the format sel+ signature + address, which are properties of the method_t structure called keys
Search for method_t, where the correspondence is as follows
name
The correspondingsel
type
The correspondingThe method signature
imp
The correspondingFunction addresses
At the same time, we found a problem: Check the _prop_list_t, clearly defines the attribute in the classification, but did not see in the underlying compiler attribute, as shown in the figure below, this is because no classification defined in the attribute set, the get method, we can set by associations (about how to set up a connection object, we will be in the expansion of the subsequent instructions)
Method 2: Search for categories in the Xcode document
If you don’t have clang, you can search for categories through the Xcode documentation
Method 3: Search for category_t from objC source code
The category_T type can also be searched through the objC source code
conclusion
In summary, the essence of a classification is a _category_T type
- There are two properties:
Name (class name)
和CLS (Class object)
- There are two
method_list_t
A list of methods of type that represent those implemented in the classificationInstance method + class method
- a
protocol_list_t
Type, representing the protocols implemented in the classification - a
property_list_t
Attribute list of type, representing the attributes defined in the classification. Generally, the attributes added in the classification are passedassociations
To implement the - It is important to note that in the classification
attribute
There is noThe set and get
methods
Classification loading
Premise: Create two categories for LGPerson: LGA and LGB