Intercept method call in Objective-C

后端 未结 10 1418
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 13:34

Can I intercept a method call in Objective-C? How?

Edit: Mark Powell\'s answer gave me a partial solution, the -forwardInvocation

10条回答
  •  佛祖请我去吃肉
    2020-12-02 14:29

    You do it by swizzling the method call. Assuming you want to grab all releases to NSTableView:

    static IMP gOriginalRelease = nil;
    static void newNSTableViewRelease(id self, SEL releaseSelector, ...) {
       NSLog(@"Release called on an NSTableView");
       gOriginalRelease(self, releaseSelector);
    }
    
    
      //Then somewhere do this:
      gOriginalRelease = class_replaceMethod([NSTableView class], @selector(release), newNSTableViewRelease, "v@:");
    

    You can get more details in the Objective C runtime documentation.

提交回复
热议问题