Next: , Up: Messaging with the GNU Objective-C runtime


8.10.1 Dynamically Registering Methods

If objc_msg_lookup() does not find a suitable method implementation, because the receiver does not implement the required method, it tries to see if the class can dynamically register the method.

To do so, the runtime checks if the class of the receiver implements the method

     + (BOOL) resolveInstanceMethod: (SEL)selector;

in the case of an instance method, or

     + (BOOL) resolveClassMethod: (SEL)selector;

in the case of a class method. If the class implements it, the runtime invokes it, passing as argument the selector of the original method, and if it returns YES, the runtime tries the lookup again, which could now succeed if a matching method was added dynamically by +resolveInstanceMethod: or +resolveClassMethod:.

This allows classes to dynamically register methods (by adding them to the class using class_addMethod) when they are first called. To do so, a class should implement +resolveInstanceMethod: (or, depending on the case, +resolveClassMethod:) and have it recognize the selectors of methods that can be registered dynamically at runtime, register them, and return YES. It should return NO for methods that it does not dynamically registered at runtime.

If +resolveInstanceMethod: (or +resolveClassMethod:) is not implemented or returns NO, the runtime then tries the forwarding hook.

Support for +resolveInstanceMethod: and resolveClassMethod: was added to the GNU Objective-C runtime in GCC version 4.6.