-
The preparatory work
-
Add class extensions, and add properties, methods
-
Add attributes in the classification, and implement set, get methods
-
-
Class extensions
-
The difference between categories and class extensions
- category
- Add new methods specifically to the class
- You can’t add a member attribute to a class, and you can’t get a member attribute if you do
- Can be achieved by
runtime
Add attributes to categories (by associating objects) - In the classification
@property
Definition variables are only generatedgetter
,setter
Method declaration, cannot generate method implementation and underlined member variables
- Class extensions
- Special categories, so to speak, also known as anonymous categories
- You can add member properties and methods to a class, but they are private
- category
-
Class extension low-level exploration
File compilation after adding class extension View the compiled file
clang -rewrite-objc main.m -o main.cpp
You 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 timeconclusion
- Class extensions are compiled as part of the class at compile time, along with the class
- Class extensions are declared only, depending on the current main class, without a.m file, which can be interpreted as a ·h file
-
-
associations
-
Verification of characteristics 2 and 4 in classification
Comment out attribute points in categories
setter
andgetter
The 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 runsetter
Methods, we preliminarily verify the attributes of the classificationgetter
,setter
Methods are declared but not implemented, -
Classification feature 3 verification
implementation
setter
andgetter
Methods throughobjc_setAssociatedObject
Set up andobjc_getAssociatedObject
The values are as follows:Discovery at runYou can assign and you can get a value -
objc_setAssociatedObject
andobjc_getAssociatedObject
Method source code analysis-
objc_setAssociatedObject
- Search the source code to see the meaning of the entry parameter
- Policy Attribute policy
objc_AssociationPolicy
_object_set_associative_reference
The underlying call to objc_setAssociatedObject from the source code is_object_set_associative_reference
Method implementation functionTake a look at_object_set_associative_reference
Source code implementationThrough the source code can be summarized as the following steps- create
AssociationsManager
variable - Get a static hash map,
AssociationsHashMap
- Check if value exists, then create an empty try_emplace
ObjectAssociationMap
To fetch a query’s key-value pair: Do not exist: Associate object – Insert empty flow - Insert an empty BucketT if the key is not present and return true
try_emplace
Method implementationLookupBucketFor
The source code to achieveHow to identifytry_emplace
Method, which method is called internally by the input attribute modifierLookupBucketFor
Method source code implementation - through
setHasAssociatedObjects
Method marks an object with an associated objectisa
Pointer to thehas_assoc
Attribute to true - The current policy and value form one
ObjcAssociation
Replace the empty space in BucketT - Mark the
ObjectAssociationMap
Is false for the first time
- create
- Map structure associated with the object
-
objc_getAssociatedObject
- Search the source code to see the meaning of the entry parameter
_object_get_associative_reference
Through the source code found the underlying call is_object_get_associative_reference
Method to implement functionalityTake a look at_object_get_associative_reference
The source code to achieve
- Search the source code to see the meaning of the entry parameter
-
-
Associated Object Flow chart
-