Skip to content

Instantly share code, notes, and snippets.

@sooop
Created May 14, 2013 10:37
Show Gist options
  • Save sooop/5575071 to your computer and use it in GitHub Desktop.
Save sooop/5575071 to your computer and use it in GitHub Desktop.
Make NSInvocation from object, selector and array of arguments.
// invocaion and operation
#import <Foundation/Foundation.h>
NSInvocation* XSMakeInvocation(id obj, SEL aSelector, NSArray *args);
int main(int argc, char const *argv[])
{
@autoreleasepool{
NSMutableString *s = [NSMutableString string];
NSInvocation *v = XSMakeInvocation(s, @selector(appendString:), @[@"This is good."]);
[v invoke];
NSLog(@"%@",s);
}
return 0;
}
NSInvocation* XSMakeInvocation(id obj, SEL aSelector, NSArray *args)
{
NSInvocation *anInvocation;
NSMethodSignature *sig;
NSUInteger i;
sig = [obj methodSignatureForSelector:aSelector];
anInvocation = [NSInvocation invocationWithMethodSignature:sig];
[anInvocation setSelector:aSelector];
[anInvocation setTarget:obj];
for(i=0;i<[sig numberOfArguments]-2;i++) {
id argv = [args objectAtIndex:i];
[anInvocation setArgument:&argv atIndex:i+2];
}
return anInvocation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment