Skip to content

Instantly share code, notes, and snippets.

@popcornylu
Created October 28, 2011 09:43
Show Gist options
  • Save popcornylu/1321980 to your computer and use it in GitHub Desktop.
Save popcornylu/1321980 to your computer and use it in GitHub Desktop.
NSInvocation addition to create instance by target, selector, and arguments
#import <Foundation/Foundation.h>
@interface NSInvocation (PopcornyLu)
+ (NSInvocation*) invocationWithTarget:(id)target
selector:(SEL)selector;
+ (NSInvocation*) invocationWithTarget:(id)target
selector:(SEL)selector
arguments:(id)firstArg, ... NS_REQUIRES_NIL_TERMINATION;
@end
#import "NSInvocation+PopcornyLu.h"
@implementation NSInvocation (PopcornyLu)
+ (NSInvocation*) invocationWithTarget:(id)target
selector:(SEL)selector
{
NSMethodSignature* sig = [[target class] instanceMethodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:target];
[invocation setSelector:selector];
return invocation;
}
+ (NSInvocation*) invocationWithTarget:(id)target
selector:(SEL)selector
arguments:(id)firstArg, ...
{
NSMethodSignature* sig = [[target class] instanceMethodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:target];
[invocation setSelector:selector];
va_list args;
id arg;
va_start(args, firstArg);
NSUInteger index = 2;
for(arg = firstArg;
arg != nil;
arg = va_arg(args, id))
{
[invocation setArgument:&arg atIndex:index++];
}
va_end(args);
return invocation;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment