Created
October 28, 2011 09:43
-
-
Save popcornylu/1321980 to your computer and use it in GitHub Desktop.
NSInvocation addition to create instance by target, selector, and 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
#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 | |
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
#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