I want to get the name of the arguments of the current function I am in so that I can prepare loading that object from the filesystem if it's not present on the current instance. (for instance if [foo dictTest] is not available I want to load it's prior saved plist version into exactly that ivar)
I want to find the file by providing the ivar name that I provided as an argument to the current function.
This is the function code:
-(NSDictionary*)getCachedDictionary:(NSDictionary*)dict{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:_cmd]];
NSString * firstArgument = nil;
[invocation getArgument:&firstArgument atIndex:2];
NSLog(@"Trying to get the objects ivar %@",firstArgument);
// right now during testing return nil
return nil;
}
As soon as the code reaches the NSLog I am getting a null value from firstArgument.
Why is that? Could it be possible that I would have to wait for the complete invocation of that current method I am in or is it actually better to create a proxy function that implicitly calls my class method via an invocation that eats the ivar name provided by setArgument so that I can use that argument string like I want?
Thanks a lot in advance!
P.S.: In this particular example I do not want to use KVC to identify the ivar and return it.
You've misunderstood the
NSInvocation
API.+[NSInvocation invocationWithMethodSignature:]
creates a newNSInvocation
that is keyed to accept arguments of the types defined by the method signature. It does not return anNSInvocation
that corresponds to the current method invocation. This is pretty easy to see why:When you create the invocation in
doFoo
for thedoBar:
method, it's obvious to see that the arguments must be empty, becausedoBar:
hasn't been executed, and thus there is no argument. Changing@selector(doBar:)
to_cmd
wouldn't magically change anything.So the next question: is there a way to get an
NSInvocation
for the current method invocation? Not that I know of.NSInvocation
is an extremely complicated class, and constructing one from the current method would be a nightmare.I strongly suggest finding a different approach to do whatever it is you want to do.