There are situations where you might want to provide an implementation of a method dynamically. For example, the Objective-C declared properties feature (see “Properties”) includes the @dynamic directive:
@dynamic propertyName; |
which tells the compiler that the methods associated with the property will be provided dynamically.
You can implement the methods resolveInstanceMethod: and resolveClassMethod: to dynamically provide an implementation for a given selector for an instance and class method respectively.
An Objective-C method is simply a C function that take at least two arguments—self and _cmd. You can add a function to a class as a method using the function class_addMethod. Therefore, given the following function:
void dynamicMethodIMP(id self, SEL _cmd) { |
// implementation .... |
} |
you can dynamically add it to a class as a method (called resolveThisMethodDynamically) using resolveInstanceMethod: like this:
@implementation MyClass |
+ (BOOL)resolveInstanceMethod:(SEL)aSEL |
{ |
if (aSEL == @selector(resolveThisMethodDynamically)) { |
class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:"); |
return YES; |
} |
return [super resolveInstanceMethod:aSEL]; |
} |
@end |
Forwarding methods (as described in “Forwarding”) and dynamic method resolution are, largely, orthogonal. A class has the opportunity to dynamically resolve a method before the forwarding mechanism kicks in. If respondsToSelector: or instancesRespondToSelector: is invoked, the dynamic method resolver is given the opportunity to provide an IMP for the selector first. If you implement resolveInstanceMethod: but want particular selectors to actually be forwarded via the forwarding mechanism, you return NO for those selectors.
Last updated: 2008-02-05