Created
November 21, 2014 06:29
-
-
Save tarunon/560add2012142ac3fd17 to your computer and use it in GitHub Desktop.
任意の返り値、任意の引数のメソッドをInvokeしてみるテスト。
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
static void * swizzed(id self, SEL aSelector, void *arg,...) | |
{ | |
NSMethodSignature *signature = [self methodSignatureForSelector:aSelector]; | |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; | |
[invocation retainArguments]; | |
[invocation setTarget:self]; | |
[invocation setSelector:aSelector]; | |
va_list argp; | |
va_start(argp, arg); | |
for (NSInteger idx = 2; idx < signature.numberOfArguments; idx++) { | |
[invocation setArgument:&arg atIndex:idx]; | |
arg = va_arg(argp, void *); | |
} | |
va_end(argp); | |
NSUInteger length = [signature methodReturnLength]; | |
if (length) { | |
void *_return = malloc(length); | |
[invocation invoke]; | |
[invocation getReturnValue:&_return]; | |
return _return; | |
} else { | |
[invocation invoke]; | |
return NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Objective-Cのメソッド呼び出し、引数の個数が複数であった場合IMPに指定したC関数側は可変数引数で全て参照可能っぽい
Objective-Cのメソッドが可変長引数の場合はいろいろ無理ぽ
これを応用するとMethodSwizzlingする際に、統一的な実装が可能になる。