开发者

Dynamic method creation in Objective-C

In the book The Pragmatic Programmer, the authors suggest that all method inputs should be validated. This allows problems with a method to be caught early and their sources traced easily.

In my Mac application, I accomplished this by creating an Assert class. This class has several class methods. These methods determine if some precondition is met, and if it is not, then an exception is thrown. A typical assertion might looks something like this:

-(void) setWidth: (int) theWidth {
    [Assert integer: width isGreaterThanInteger: 0];
    width = theWidth;
}

This works really well, and significantly reduced the amount of time I've spend bug hunting. However, I've noticed lately some of the assertion methods are very useful as predicates. For example, my integer:isGreaterThanInteger:andLessThanInteger: and my stringIsNotEmpty: methods are equally useful. To this end, I created a second class Predicate, which I filled with several of my more useful predicate methods. So I took the logic from the assert methods, and moved it into Predicate, and then rewrote my Assert methods like the following:

if ![Predicate predicateMethod]
    throw exception

This has turned into a maintenance nightmare. If I change the name of a method name in Predicate, I must also change it in Assert to stay consistent. If I update the documentation of an Assert method, then I must do the same to a Predicate method.

Ideally, I would like the reconstruct the Assert class so that when any method is called on it, it intercepts the selector. The Predicate class can then be checked to see if it responds to the selector, and if it does, the method is called on Predicatewith the same arguments that were pas开发者_Python百科sed into the Assert method. If the Predicate method returns false, then an exception is thrown.

Is there a way to do this in Objective-C?

Thanks.


You could use -forwardingTargetForSelector: to simply forward the method to another object, but if you want advanced behavior (like checking the return value to see if it's false), you may need to use -forwardInvocation:. (However, note that the documentation says this is "much more expensive" than the former option.)


If you're using pure Objective-C, you should see the "Forwarding" discussion here. It basically describes how to do exactly what you want, including example code.

If you're using Cocoa then you might have to use forwardInvocation: instead.


I ended up overriding resolveClassMethod:. While overriding forwardInvocation might have worked (I would have had to figure out some way to override it for the class object), resolveClassMethod: seems like it's the easier and more efficient method. Here's what my final implementation ended up looking like:

#import "Assert.h"
#import "Predicate.h"
#include <objc/objc-runtime.h>

void handlePredicateSelector(id self, SEL _cmd, ...);

@implementation Assert

+(void) failWithMessage: (NSString *) message
{
    NSLog(@"%@", message);
    [NSException raise:@"ASSERTION FAILURE" format:message];
}

+(void) fail
{
    [Assert failWithMessage:@"An unconditional failure has been detected."];
}

+(BOOL) resolveClassMethod: (SEL) selector
{
    if ([(id) [Predicate class] respondsToSelector:selector])
    {
        /*
         The meta class fix was taken from here: http://iphonedevelopment.blogspot.com/2008/08/dynamically-adding-class-objects.html
         */

        //get the method properties from the Predicate class
        Class predicateMetaClass = objc_getMetaClass([[Predicate className] UTF8String]);
        Method predicateMethod = class_getClassMethod(predicateMetaClass, selector);
        const char *encoding = method_getTypeEncoding(predicateMethod);

        Class selfMetaClass = objc_getMetaClass([[self className] UTF8String]);
        class_addMethod(selfMetaClass, selector, (IMP) handlePredicateSelector, "B@:?");

        return YES;
    }
    return [super resolveClassMethod:selector];
}

@end

void handlePredicateSelector(id self, SEL _cmd, ...)
{
    //get the number of arguments minus the self and _cmd arguments
    NSMethodSignature *predicateMethodSignature = [(id) [Predicate class] methodSignatureForSelector:_cmd];
    NSUInteger numberOfArguments = [predicateMethodSignature numberOfArguments] - 2;

    NSInvocation *predicateInvocation = [NSInvocation invocationWithMethodSignature:predicateMethodSignature];
    [predicateInvocation setTarget:[Predicate class]];
    [predicateInvocation setSelector:_cmd];

    va_list ap;
    va_start(ap, _cmd);

    for (int i = 0; i < numberOfArguments; i++)
    {
        void *arg = va_arg(ap, void *);
        [predicateInvocation setArgument:&arg atIndex:i+2];
    }

    va_end(ap);

    BOOL returnValue;
    [predicateInvocation invoke];
    [predicateInvocation getReturnValue:&returnValue];

    //determine if the assertion is true
    if (!returnValue)
    {
        [Assert failWithMessage:[NSString stringWithFormat: @"The following assertion failed: %@", NSStringFromSelector(_cmd)]];
    }
}

The only thing I couldn't really figure out was how to get the type encoding from the method signature. It didn't seem to affect the output of the methods, but I would like to fix it if I can.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜