Classification (category)

From the previous analysis of the structure of a class, we know that the class is essentially a structure. Today we will look at the structure and loading process of the classification.

Classification structure

  • In simple terms, classes are also structures, independent of classes. Instead of adding extensions directly to a class, we create a classification structure that puts all extensibility into the class and adds it dynamically at Runtime.

To create a category, go to the. M directory on your terminal and export a c++ category code file by searching for “category_t {“.

Xcrun-sdk iphoneos-arch arm64 Clang-rewrite-objc category name.m

Objc-runtime_new objc-runtime_new objc-runtime_new objc-runtime_new objc-runtime_new objc-Runtime_new

struct category_t { const char *name; classref_t cls; WrappedPtr<method_list_t, PtrauthStrip> instanceMethods; WrappedPtr<method_list_t, PtrauthStrip> classMethods; struct protocol_list_t *protocols; struct property_list_t *instanceProperties; // Fields below this point are not always present on disk. struct property_list_t *_classProperties; method_list_t *methodsForMeta(bool isMeta) { if (isMeta) return classMethods; else return instanceMethods; } property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi); protocol_list_t *protocolsForMeta(bool isMeta) { if (isMeta) return nullptr; else return protocols; }};Copy the code
  • Classification structure
category_t note
name The name of the class
instanceMethods List of object methods
classMethods List of class methods
protocols Agreement list
instanceProperties Property list
.

As can be seen from the structure diagram of classification, instance methods, class methods, protocols and member variables in classification are stored by a structure named category_T, which is relatively independent from the main class in the compilation stage.

Note: There is no member list (IVars) found in the category_t structure, as you can see that the category does not allow adding member variables by default.

How are categories loaded into the main class

Classification data loading process
1. Load all Category data of the main class through the Runtime
2. Merge all Category object methods, class methods, attributes, and protocol data into one large array
3. Insert the class data (methods, properties, protocols) from the large array into the main class

The later compiled Category data is placed in front of the array, and the classification data is placed in front of the main class data.

When the data is actually loaded, the methods of the class are called if the class has the same methods as the main class.

If different classes have the same method, the method that later participates in compiling the class is called.

The entire loading process is done through the Runtime.

The loading of Runtime affects the startup of the program, so it is necessary to use the classification properly. If the runtime is overused, the startup time of the program will be increased and the user experience will be affected.

associations

  • A category cannot directly define a member variable

  • @property defines properties in a class. It simply generates declarations of setter and getter methods. It does not generate “_ property name “member variables

  • You can do it indirectly through the associated property in the Runtime by assigning it to the setter, getting it to the getter

Here’s an example:

#import "FRPerson.h"

@interface FRPerson (Eat)

@property (nonatomic, strong) NSArray *foots;

-(void)eat;

@end
Copy the code
#import "FRPerson+Eat.h" #import < objc/runtime.h > const static char *associateID = "foots"; @implementation FRPerson (Eat) -(void) Eat {NSLog(@" Eat,%@", self.foots[0]); } #pragma mark - AssociateObject -(NSArray *)foots{return objc_getAssociatedObject(self, associateID); } -(void)setFoots:(NSArray *)foots{ objc_setAssociatedObject(self, associateID, foots, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @endCopy the code

In the classification can not add member variables, so the use of the associated object indirectly complete the function of the attribute, but can only use “. Syntax assigns values.

You can actually use a setter or setter method address for a key that’s associated with an object, so you can just use @selector(foots), which is a little bit more resource-efficient.

We will not discuss the principle of associative objects. Later, we’ll have time to study the principles of associative objects. I believe there are also a lot of relevant information on the network, readers interested can also search to see.

What is it like to create a serial-oc object

What is the flow of series -OC method call

How is series – KVO&KVC realized