• The preparatory work

    1. Add class extensions, and add properties, methods

    2. Add attributes in the classification, and implement set, get methods

  • Class extensions

    • The difference between categories and class extensions
      • category
        1. Add new methods specifically to the class
        2. You can’t add a member attribute to a class, and you can’t get a member attribute if you do
        3. Can be achieved byruntimeAdd attributes to categories (by associating objects)
        4. In the classification@propertyDefinition variables are only generatedgetter,setterMethod declaration, cannot generate method implementation and underlined member variables
      • Class extensions
        1. Special categories, so to speak, also known as anonymous categories
        2. You can add member properties and methods to a class, but they are private
    • Class extension low-level exploration

      File compilation after adding class extension View the compiled fileclang -rewrite-objc main.m -o main.cppYou can also run source validation if you find that member attributes and methods from the class extension have been added to the class at compile time

      conclusion

      1. Class extensions are compiled as part of the class at compile time, along with the class
      2. Class extensions are declared only, depending on the current main class, without a.m file, which can be interpreted as a ·h file
  • associations

    1. Verification of characteristics 2 and 4 in classification

      Comment out attribute points in categoriessetterandgetterThe method assigns values to attributes in the classification and then runsThe assignment compilation was found to pass, but the crash message was not found after the runsetterMethods, we preliminarily verify the attributes of the classificationgetter,setterMethods are declared but not implemented,

    2. Classification feature 3 verification

      implementationsetterandgetterMethods throughobjc_setAssociatedObjectSet up andobjc_getAssociatedObjectThe values are as follows:Discovery at runYou can assign and you can get a value

    3. objc_setAssociatedObjectandobjc_getAssociatedObjectMethod source code analysis
      • objc_setAssociatedObject
        1. Search the source code to see the meaning of the entry parameter
        2. Policy Attribute policyobjc_AssociationPolicy
        3. _object_set_associative_reference

          The underlying call to objc_setAssociatedObject from the source code is_object_set_associative_referenceMethod implementation functionTake a look at_object_set_associative_referenceSource code implementationThrough the source code can be summarized as the following steps
          1. createAssociationsManagervariable
          2. Get a static hash map,AssociationsHashMap
          3. Check if value exists, then create an empty try_emplaceObjectAssociationMapTo fetch a query’s key-value pair: Do not exist: Associate object – Insert empty flow
          4. Insert an empty BucketT if the key is not present and return truetry_emplaceMethod implementation LookupBucketForThe source code to achieveHow to identifytry_emplaceMethod, which method is called internally by the input attribute modifierLookupBucketForMethod source code implementation
          5. throughsetHasAssociatedObjectsMethod marks an object with an associated objectisaPointer to thehas_assocAttribute to true
          6. The current policy and value form oneObjcAssociationReplace the empty space in BucketT
          7. Mark theObjectAssociationMapIs false for the first time
        4. Map structure associated with the object
      • objc_getAssociatedObject
        1. Search the source code to see the meaning of the entry parameter

        2. _object_get_associative_reference

          Through the source code found the underlying call is_object_get_associative_referenceMethod to implement functionalityTake a look at_object_get_associative_referenceThe source code to achieve
    • Associated Object Flow chart