Where did this approach come from?

  • lookUpImpOrForward jump

  • Log_and_fill_cache jump Populates the cache

  • logMessageSend jump

  • instrumentObjcMessageSends jump

So the extern void instrumentObjcMessageSends (BOOL flag); I’m just going to take the inner method, and I’m going to extend it, and I’m going to pass YES/NO

Next in HLPerson. Add forwardingTargetForSelector method, fast forward m file

/ / output
-[HLPerson forwardingTargetForSelector:] *** sayNB
 -[HLPerson sayNB]: unrecognized selector sent to instance 0x100a72f70
Copy the code

To modify the return value, the HLTeacher class has – (void)teacherSay methods — fast forward

// Fast forward
- (id) TargetForSelector:(SEL)aSelector{
    
    NSLog(@"%s *** %@",__func__,NSStringFromSelector(aSelector));
    
    return [HLTeacher alloc];
    
}
Copy the code
/ / output
-[HLPerson forwardingTargetForSelector:] *** sayNB
-[HLTeacher teacherSay]
Copy the code
// Slow forwarding
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
   
    NSLog(@"%s --- %@",__func__,NSStringFromSelector(aSelector));
   
    if (aSelector == @selector(sayNB)) {
        
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
 
    }
    
    return [super methodSignatureForSelector:aSelector];
    
}
Copy the code
/ / output
 -[HLPerson methodSignatureForSelector:] --- sayNB

- (void)forwardInvocation:(NSInvocation *)anInvocation{
    
    NSLog(@ % @ # # # % @ "",anInvocation.target,NSStringFromSelector(anInvocation.selector));
    
}
-[HLPerson methodSignatureForSelector:] --- sayNB
 <HLPerson: 0x101547e20> ### sayNB
Program ended with exit code: 0
Copy the code
// Add respondsToSelector
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    
// NSLog(@"%@ ### %@",anInvocation.target,NSStringFromSelector(anInvocation.selector));
    
    HLTeacher *t = [HLTeacher alloc];
    
    if ([self respondsToSelector:anInvocation.selector]) {
        [anInvocation invoke];
    }else if ([t respondsToSelector:anInvocation.selector]){
        [anInvocation invokeWithTarget:t];
    }else{
        NSLog(@"%s --- %@",__func__,NSStringFromSelector(anInvocation.selector)); [anInvocation invoke]; }}Copy the code

Now annotate the fast and slow methods above and take a look at LLDB

// The key to the error
  frame #10: 0x00007fff2059b90b CoreFoundation`___forwarding___ + 1448
  frame #11: 0x00007fff2059b2d8 CoreFoundation`_CF_forwarding_prep_0 + 120
Copy the code

To be continued…