Let's say I have the following method:
-(void)methodWithVar1:(NSString*)var1 var2:(NSString*)var2;
Now of course I can access var1 and var2 within methodWithVar1:var2: by using the variable names directly. But what I'd like to do is something like methodArgs[0] and methodArgs[1] to access the variables.
Is there a way to do this? I've looked at runtime.h but can't see anything that helps.
Why do I want to do this?
In certain circumstances, when a method is called, I want to prevent the method executing, but allow it to execute at another moment in time. I'm doing this by creating an NSInvocation object that allows me to 're-call' the method when I would prefer it. However, NSInvocation requires that I call setArgument:atIndex:, which I have to do manually. If the method every changes, then the population of NSInvocation needs to be updated. I want to avoid updating it manually and have a generic way of doing it.
Example
-(void)methodWithVar1:(NSString*)var1 var2:(NSString*)var2{
if (someCheckToSeeIfICannotRun) {
NSMethodSignature * methodSignature =
[self.class instanceMethodSignatureForSelector:_cmd];
KWInvocation * invocation =
[KWInvocation invocationWithMethodSignature:methodSignature];
[invocation.invocation setTarget:self];
[invocation.invocation setSelector:_cmd];
[invocation.invocation setArgument:&var1 atIndex:2];// This and
[invocation.invocation setArgument:&var2 atIndex:3];// this, I would prefer to make generic
[invocation.invocation retainArguments];
//
// Store invocation somewhere so I can call it later...
//
}
else {
// Let the method run as normal
}
}