Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We know that if we want to save instance methods and class methods, we need to implement the resolveInstanceMethod and resolveClassMethod methods in the class. If you want to save every class method, you need to implement both methods in every class. Is there a better way to do this? The search flow for instance methods goes through the class object, the parent class, and finally the root class. The process of finding a class method goes through the metaclass, the root metaclass, and finally the root class as well. In the root class, resolveInstanceMethod is called when either the instance method or the class method is not found. So we just need to implement the resolveInstanceMethod method in NSObject to save all the instance methods and class methods. To create the NSObject+LG class, write the following code:

#import "NSObject+LG.h" #import <objc/runtime.h> @implementation NSObject (LG) -(void)say666{ NSLog(@"666"); } + (BOOL)resolveInstanceMethod (SEL) SEL {if(SEL ==@selector(sayNB)){NSLog(@"resolveInstanceMethod: %@, %@", self, NSStringFromSelector(sel)); IMP imp = class_getMethodImplementation(self, @selector(say666)); Method methodSay666 = class_getInstanceMethod(self, @selector(say666)); const char *type = method_getTypeEncoding(methodSay666); return class_addMethod(self, @selector(sayNB), imp, type); } return NO; } @endCopy the code

In the main function, the sayNB class method of LGPerson is called along with the sayNB method of the instance object per

int main(int argc, const char * argv[]) { @autoreleasepool { [LGPerson sayNB]; LGPerson *per= [LGPerson alloc]; [per sayNB]; } return 0; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / output results: instance methods - - say666 say666 instance methodsCopy the code

Advantages of this scheme:

  • Any class that inherits fromNSObjectAll of its methods can be listened on
  • We can name custom methods according to the specified policy, and then listen according to the same policy. If a method that meets the policy cannot be found, it can be reported to the server, so that the developer can get the feedback of the problem in the first time
  • inNSObjectAll methods are listened for uniformly in the classification, which is consistent with AOP’s section-oriented design pattern
    • traditionalOOPThe object-oriented design pattern, although the division of labor of each object is very clear, but some of the same behavior between them, will lead to a lot of redundant code. If we extract it and create a common class for inheritance, we are bound to have strong dependencies and high coupling
    • whileAOPAdvantages for primitive classes and objects without intrusion, as long as well maintainedNSObjectThe listening method in the classification can be

Disadvantages:

  • Writing a large number of judgment conditions in the monitoring method is not conducive to search and maintenance
  • All methods are listened on, which contains a large number of system methods, causing a performance drain
  • inNSObjectThe message forwarding process provided by the system cannot be triggered

For fault tolerance, we should give developers more room for error tolerance. So the “optimized” solution we provide using the AOP design pattern is not really a good solution in this scenario.