Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

Resurrected code - mullePreviousInstanceMethodForSelector

A mail exchange on the Cocoa Developers mailing list made me dig out some old code I had lying around here from a company long deceased and by an unspecified author, who I suspect might have been him. I fixed it up a little and here it is. Don't complain if it's slowing down your code :P

#import <Foundation/Foundation.h>


@interface NSObject ( MullePreviousIMP)

+ (IMP) mullePreviousInstanceMethodForSelector:(SEL) sel;

@end

and the implementation

#import "NSObject+MullePreviousIMP.h"

#import <objc/objc-class.h>


@implementation NSObject ( MullePreviousIMP)

+ (IMP) mullePreviousInstanceMethodForSelector:(SEL) sel
{
   struct objc_method_list   *mlist;
   struct objc_method        *p;
   void     *rover;
   int      i;
   BOOL     flag;
   IMP      imp;
   Class    curClass;

   flag  = NO;
   rover = 0;

   imp = [self instanceMethodForSelector:sel];

   for( curClass = self; curClass; curClass = [curClass superclass])
   {
      while( (mlist = class_nextMethodList( curClass, &rover)) != NULL)
      {
         for( i = mlist->method_count - 1; i >= 0 ; i--)
         {
            p = &mlist->method_list[ i];
            if( p->method_name == sel)
            {
               if( p->method_imp == imp)
               {
                  flag = YES;
                  break;
               }

               if( flag)
                  return( p->method_imp);

               break;
            }
         }
      }
   }

   return( NULL);
}

Here is an archive with some test code: MullePreviousIMP.tgz