Created
May 14, 2013 10:37
-
-
Save sooop/5575071 to your computer and use it in GitHub Desktop.
Make NSInvocation from object, selector and array of arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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