Theocacao
Leopard
Design Element
Comment on "Dynamic Objective-C with NSInvocation"
by Zak — Apr 07
an excellent post. it's proven useful for catching accidental missing methods. here's a (perhaps naive) implementation:

- (void) forwardInvocation: (NSInvocation*)invocation
{
NSString* missingMethod = NSStringFromSelector([invocation selector]);
NSObject* object = [invocation target];
[invocation setArgument: &missingMethod atIndex: 2];
[invocation setArgument: &object atIndex: 3];
[invocation setSelector: NSSelectorFromString(@"methodMissing:object:arguments:")];
return [invocation invokeWithTarget:self];
}

-(void) methodMissing: (NSString*)method object:(id)object arguments:(NSMutableArray*)arguments {
println(@"%@ is missing %@", ((NSObject*)object).description, method);
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
NSMethodSignature* sig = nil;
if ([self respondsToSelector:sel]) {
sig = [[self class] instanceMethodSignatureForSelector:sel];
}
else {
SEL methodMissingSelector = NSSelectorFromString(@"methodMissing:object:arguments:");
sig = [[self class] instanceMethodSignatureForSelector: methodMissingSelector];
}
return sig;
}


I wonder though if you know how to capture the arguments sent to a method without knowing the method signature? In this case, I'd like to actually capture the arguments being sent to the unknown method, put them into an array, and then send that along to my methodMissing method for later use. something like Ruby's method_missing.

any thoughts?
Back to "Dynamic Objective-C with NSInvocation"
Design Element

Copyright © Scott Stevenson 2004-2015