Saturday, May 11, 2013

NSInvocation invoke example ios


invoke

Sends the receiver’s message (with arguments) to its target and sets the return value.
- (void)invoke
Discussion( NSInvocation invoke example )
You must set the receiver’s target, selector, and argument values before calling this method.
( NSInvocation invoke example )
- (void)forwardInvocation:(NSInvocation *)invocation {
    id target = [invocation target];
    Class targetClass = classWithTheImpIWant();
    Class originalClass = objc_setClass(target, targetClass);
    [invocation invoke];
    objc_setClass(target, originalClass);
}
( NSInvocation invoke example )
@implementation Car

- (void)log {
    NSLog(@"-[Vehicle log] invoked on an instance of %@", NSStringFromClass([self class]));
}

- (void)runVehiclesLog {
    [super log];
}

- (void)runInvocationThatTargetsVehicle {
    SEL selector = @selector(runVehiclesLog);
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
    [invocation setTarget:self];
    [invocation setSelector:selector];
    [invocation invoke];
}
@end