NSInvocationを組み立てるのが面倒くさかったので1メソッド呼び出しにしてみた
引数付きでSelectorを保持したいと思い、調べてみたらNSInvocationが良いらしいということがわかった。
が、いちいちSignature作って、setTargetして、引数セットして…というのが面倒なのでビルドメソッドを作った。
一応動いてはいるけど、これが安全なのか検証はしていません。
悩むのは、どこにこのメソッドを記述しておくかということ。
NSInvocationのカテゴリにでもするか?
- -
追記1 2012年11月19日 12:04
NSObjectのカテゴリとして、selfを省略する形に変更。
NSObject + BuildInvocation.h
- (NSInvocation *)invocationWithSelector:(SEL)selector args:(void *)arg1, ... NS_REQUIRES_NIL_TERMINATION;
NSObject + BuildInvocation.m
- (NSInvocation *)invocationWithSelector:(SEL)selector args:(void *)arg1, ... { va_list params; va_start(params, arg1); void *arg = arg1; NSMethodSignature *signature = [self methodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation retainArguments]; [invocation setTarget:self]; [invocation setSelector:selector]; for (int i=2; arg != nil; i++) { [invocation setArgument:arg atIndex:i]; arg = va_arg(params, void*); } va_end(params); return invocation; }
-
- -