Skip to content

Instantly share code, notes, and snippets.

@tarunon
Created November 21, 2014 06:29
Show Gist options
  • Save tarunon/560add2012142ac3fd17 to your computer and use it in GitHub Desktop.
Save tarunon/560add2012142ac3fd17 to your computer and use it in GitHub Desktop.
任意の返り値、任意の引数のメソッドをInvokeしてみるテスト。
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;
}
}
@tarunon
Copy link
Author

tarunon commented Nov 21, 2014

Objective-Cのメソッド呼び出し、引数の個数が複数であった場合IMPに指定したC関数側は可変数引数で全て参照可能っぽい
Objective-Cのメソッドが可変長引数の場合はいろいろ無理ぽ
これを応用するとMethodSwizzlingする際に、統一的な実装が可能になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment