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] Throughclang
  • [Method 2] PassXcodeDocument searchCategory
  • [Method 3] PassobjcThe source code searchcategory_t

Method 1: Clang

  • Clang-rewrite-objc LGPerson+ lga. m -o LGPerson+ lga. CPP

    • Among themclassificationIs of type_category_t
    • The penultimate 0 of the category is zeroNo agreement, soThe assignment is 0

Search for struct _category_t as shown below

  • There are two method_list_t, respectivelyInstance 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

  • nameThe correspondingsel
  • typeThe correspondingThe method signature
  • impThe 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 twomethod_list_tA list of methods of type that represent those implemented in the classificationInstance method + class method
  • aprotocol_list_tType, representing the protocols implemented in the classification
  • aproperty_list_tAttribute list of type, representing the attributes defined in the classification. Generally, the attributes added in the classification are passedassociationsTo implement the
  • It is important to note that in the classificationattributeThere is noThe set and getmethods

Classification loading

Premise: Create two categories for LGPerson: LGA and LGB