#define invokeSupersequent(...) \ ([self getImplementationOf:_cmd after:impOfCallingMethod(self, _cmd)]) (self, _cmd ,##__VA_ARGS__)#define invokeSupersequentNoParameters() \ ([self getImplementationOf:_cmd after:impOfCallingMethod(self, _cmd)]) (self, _cmd)IMPimpOfCallingMethod(idlookupObject,SELselector){NSUIntegerreturnAddress=(NSUInteger)__builtin_return_address(0);NSUIntegerclosest=0;// Iterate over the class and all superclassesClasscurrentClass=object_getClass(lookupObject);while(currentClass){// Iterate over all instance methods for this classunsignedintmethodCount;Method*methodList=class_copyMethodList(currentClass,&methodCount);unsignedinti;for(i=0;i<methodCount;i++){// Ignore methods with different selectorsif(method_getName(methodList[i])!=selector){continue;}// If this address is closer, use it insteadNSUIntegeraddress=(NSUInteger)method_getImplementation(methodList[i]);if(address<returnAddress&&address>closest){closest=address;}}free(methodList);currentClass=class_getSuperclass(currentClass);}return(IMP)closest;}@interfaceNSObject(SupersequentImplementation)-(IMP)getImplementationOf:(SEL)lookupafter:(IMP)skip;@end@implementationNSObject(SupersequentImplementation)// Lookup the next implementation of the given selector after the// default one. Returns nil if no alternate implementation is found.-(IMP)getImplementationOf:(SEL)lookupafter:(IMP)skip{BOOLfound=NO;ClasscurrentClass=object_getClass(self);while(currentClass){// Get the list of methods for this classunsignedintmethodCount;Method*methodList=class_copyMethodList(currentClass,&methodCount);// Iterate over all methodsunsignedinti;for(i=0;i<methodCount;i++){// Look for the selectorif(method_getName(methodList[i])!=lookup){continue;}IMPimplementation=method_getImplementation(methodList[i]);// Check if this is the "skip" implementationif(implementation==skip){found=YES;}elseif(found){// Return the match.free(methodList);returnimplementation;}}// No match found. Traverse up through super class' methods.free(methodList);currentClass=class_getSuperclass(currentClass);}returnnil;}@end