@interface ClassConstructor : NSObject {
@private
NSInvocation *_invocation;
NSMethodSignature *_method;
Class _class;
}
@property (nonatomic, retain) NSInvocation *_invocation;
@property (nonatomic, retain) NSMethodSignature *_method;
- (id)initWithClass:(Class)iClass method:(NSString *)iMethod, ...;
- (id)create;
@end
@implementation ClassConstructor
@synthesize _invocation, _method;
- (id)initWithClass:(Class)iClass method:(NSString *)iMethod, ... {
if (self = [super init]) {
_class = iClass;
if (iMethod) {
SEL aSelector = NSSelectorFromString(iMethod);
self._method = [iClass instanceMethodSignatureForSelector:aSelector];
self._invocation = [NSInvocation invocationWithMethodSignature:_method];
[_invocation retainArguments];
[_invocation setSelector:aSelector];
NSUInteger aCount = [_method numberOfArguments];
va_list anArgList;
va_start(anArgList, iMethod);
   for (NSUInteger anIndex = 2; anIndex     void *aPointerArg = va_arg(anArgList, void *);     [_invocation setArgument:&aPointerArg atIndex:anIndex];    }    va_end(anArgList);   }  }  return self; } - (void)dealloc {  [_invocation release];  [_method release];  [super dealloc]; } - (id)create {  id aReturnVal = nil;    // alloc and set the target  [_invocation setTarget:[_class alloc]];    // invoke the constructor  [_invocation invoke];    // get the result  if (_method.methodReturnLength) {   [_invocation getReturnValue:&aReturnVal];  }  return aReturnVal; } @end
ClassConstructor *aClassConstructor = [[[ClassConstructor alloc] initWithClass:[FooController class] method:@"initWithStyle:", UITableViewStylePlain] autorelease];
No comments:
Post a Comment