Type Method

resolveInstanceMethod:

Dynamically provides an implementation for a given selector for an instance method.

Declaration

+ (BOOL)resolveInstanceMethod:(SEL)sel;

Parameters

name

The name of a selector to resolve.

Return Value

YES if the method was found and added to the receiver, otherwise NO.

Discussion

This method and resolveClassMethod: allow you to dynamically provide an implementation for a given selector.

An Objective-C method is simply a C function that take at least two arguments—self and _cmd. Using the class_addMethod function, you can add a function to a class as a method. Given the following function:

void dynamicMethodIMP(id self, SEL _cmd)
{
    // implementation ....
}

you can use resolveInstanceMethod: to dynamically add it to a class as a method (called resolveThisMethodDynamically) like this:

+ (BOOL) resolveInstanceMethod:(SEL)aSEL
{
    if (aSEL == @selector(resolveThisMethodDynamically))
    {
          class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");
          return YES;
    }
    return [super resolveInstanceMethod:aSel];
}

Special Considerations

This method is called before the Objective-C forwarding mechanism is invoked. If respondsToSelector: or instancesRespondToSelector: is invoked, the dynamic method resolver is given the opportunity to provide an IMP for the given selector first.

See Also

Dynamically Resolving Methods

+ resolveClassMethod:

Dynamically provides an implementation for a given selector for a class method.