Category (Category) :

concept

A Category is a syntax unique to OC that represents a pointer to the structure of a Category. In principle, it can only add methods, not member (instance) variables.

  1. You can write at sign property in a class, but it won’t generatesetter/getterMethod, and no implementation or private member variables are generated (warning at compile time);
  2. You can access properties in the original class. H in the classification;
  3. If there is a method in the class with the same name as the original class, the method in the class will be called first, that is, the method in the original class will be ignored. So the precedence of method calls with the same name isCategory > This class > Parent class. So try not to overwrite existing classes in your development;
  4. If more than one class has a method with the same name as the original class, the compiler decides who executes when calling the method. The compiler executes the method in the last class to participate in the compilation.

We know that if we declare a property with @Property in a class, the compiler will automatically generate it for us
_ Member variablesand
setter/getterBut there is no property list at all in the pointer structure of the classification. So declaring a property in a class with @property doesn’t generate it
_ Member variablesIt can’t generate
setter/getter.


So the conclusion is: we can declare a property with @property, and it will compile and run without crashing as long as we don’t use it. But if you call it
_ Member variablesand
setter/getterMethod, error is inevitable.


Since the root cause of the error is the use of the system did not generate
setter/getterMethod, can be manually added
setter/getterTo avoid a crash and complete the call?


Well, you can. Since OC is a dynamic language, the real implementation of the method is through
runtimeIt’s done, although the system won’t give it to us
setter/getterBut we can pass
runtimeManually add
setter/getterMethods.

The code implementation is as follows:

#import <objc/runtime.h>

static NSString *nameWithSetterGetterKey = @"nameWithSetterGetterKey"; Implementation Programmer (Category) {// Implementation Programmer (Category) {// Implementation Programmer (Category) {setNameWithSetterGetter:(NSString *)nameWithSetterGetter { objc_setAssociatedObject(self, &nameWithSetterGetterKey, nameWithSetterGetter, OBJC_ASSOCIATION_COPY); } // Run time implement getter method - (NSString *)nameWithSetterGetter {return objc_getAssociatedObject(self, &nameWithSetterGetterKey);
}

@endCopy the code

Note, however, that the above code simply implements the setter/getter method manually, but calling the _ member variable still returns an error.

Class Extension

Extension is a special case of Category. Class extension is called “anonymous classification” because it lacks only the name of the classification. In fact, during development, we use it almost every day. For some it’s like the most familiar stranger.

@interface XXX () // Private property // private Method (if not implemented, it will be reported at compile time,Method definitionfor 'XXX'Not found) @ the endCopy the code

Function:

Add extra classes that don’t have variables, methods, and attributes that are generic class extensions and write them to the.m file and write the generic private attributes to the.m file


The difference between categories and class extensions:

  1. In principle, only methods can be added to a class (the only reason to add attributes is throughruntimeTo solve withoutsetter/getterIs just a question);
  2. Class extensions can add not only methods, but also instance variables (or attributes), which are of type @private by default.
  3. Use scopes only on their own classes, not subclasses or elsewhere);
  4. The compiler raises an alarm if a method declared in a class extension is not implemented, but does not raise any alarm if a method declared in a class extension is not implemented. This is because class extensions are added to the class at compile time, whereas categories are added to the class at run time.
  5. Class extensions cannot have a separate implementation part (the @implementation part) like classes do, that is, the methods declared by class extensions must rely on the implementation part of the corresponding class.
  6. Class extension methods defined in.m files are private, and class extension methods defined in.h files (header files) are public. Class extensions are a great way to declare private methods in.m files.