« My Lombard died and "buying a G5 iMac might not be such a good idea" | Main | Obj-C changes »

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

About

This page contains a single entry from the blog posted on April 26, 2005 1:04 AM.

The previous post in this blog was My Lombard died and "buying a G5 iMac might not be such a good idea".

The next post in this blog is Obj-C changes.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34