Design Element
Comment on "Dynamic Objective-C with NSInvocation"
by Trenton Ahrens — Jun 25
a function i threw together for a project to store a message for later use

NSInvocation* storeMessage(id target, SEL selector, ...) { NSMethodSignature *sig = [[target class] instanceMethodSignatureForSelector:selector]; NSInvocation *nsInvocation = [NSInvocation invocationWithMethodSignature:sig]; [nsInvocation setTarget:target]; [nsInvocation setSelector:selector]; id eachObject; int cur = 2; va_list args; va_start(args, selector); while( eachObject = va_arg(args, id) ) { [nsInvocation setArgument:&eachObject atIndex:cur]; cur++; } va_end(args); [nsInvocation retain]; return nsInvocation; }

NSInvocation* inv = storeMessage(targetObject, @selector(functionToCall), arg1, arg2, arg3, nil); [inv invoke];
Back to "Dynamic Objective-C with NSInvocation"
Design Element

Copyright © Scott Stevenson 2004-2015