ios - Obj-C introspection: How can a method reference arguments it's called with -


this intentional "fork" of question: obj-c introspection: how can method reference own selector?

i need exact same thing op, whereas didn't need pass arguments, do.



have way call method multiple unknown arguments (nsinvocation). have convenience methods me, in question. however, having duplicate effort each method in each class required has led maintenance issues, , hellish debugging when wrong argument passed (or in wrong order) convenience method.

that itself, relative minor issue, 1 i've been grappling 2 years, , solve. having one-line macro (like @michael57) call convenience method, automatically inspecting selector (using _cmd) , arguments (using ?) , calling convenience method wished.


note methods fixed argument lists , not variadic. i'm not sure if va_args approach work inspecting arguments or not.

so both code management perspective, , purely out of technical curiosity (if _cmd exists selectors, there must arguments?) know how standard obj-c method arguments can introspected.


hope obvious i'm missing. help!


edit: sample code: basic purpose of i'm trying run methods called on instance, in single thread. have convenience method in nsobject category:

- (void)performselectorinbackground:(sel)aselector waituntildone:(bool)waituntildone withargumentlocations:(void *)firstarg,...;

runs called selector on target object in specific background thread mapped instance. avoid sticky synchronization issues otherwise occur.

maybe kludgy in first place, i'm way past that. code integrated in many places. :)

the "withargumentlocations" variadic arguments list list of pointer locations can take in both scalar values , regular obj-c objects, making usual task of calling selector in background variable number , type of arguments easy 1-line affair.


anyway, actual use case below:


- (void)loginrequestwithusername:(nsstring *)username andpassword:(nsstring *)password {

if(![self isinbackgroundthread]) {     [self performselectorinbackground:@selector(loginrequestwithusername:andpassword:) waituntildone:no withargumentlocations:&username, &password, nil];     return; } 



[self isinbackgroundthread] convenience method checks if current execution thread "assigned" 1 instance.

so approach has worked fine individual method calls, want expand methods within class. has led issues when "argumentlocations" not substituted properly, or not in right order, etc leading hellish debugging.


why i'd replace above block of code little macro, can paste in every method achieve need.


why macro should able query own selector (_cmd) , arguments (using ?) , pass these along performselectorinbackground:waituntildone:withargumentlocations: method.

the first thing comes mind getting nsinvocation not implement method, , -forwardinvocation: called constructed nsinvocation. guess not directly helpful since need implement method , stuff in method. however, perhaps in -forwardinvocation: invoke invocation on object ("forwarding invocation"), method on object real stuff. store invocation in place that method can access if necessary. or maybe can "re-run" logic want in -forwardinvocation: itself.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -