Following code throws an exception.
vcClass
is a Class
object (inheritor from UIViewController
). Self
contains my implementation of viewWillAppear:
SEL viewWillAppearSEL = @selector(viewWillAppear:);
IMP viewWillAppearWithSuperIMP = [self methodForSelector:viewWillAppearSEL];
class_addMethod(vcClass, viewWillAppearSEL, viewWillAppearWithSuperIMP, @encode(BOOL));
NSMethodSignature *methodSignature = [vcClass instanceMethodSignatureForSelector:viewWillAppearSEL];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setSelector:viewWillAppearSEL];
With message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSInvocation setArgument:atIndex:]: index (1) out of bounds [-1, -1]
Additional info: iOS5, ARC.
Can someone explain me what's wrong?
UPDATED:
This code code gives me responds message. So my class object is correct
[vcClass instancesRespondToSelector:viewWillAppearSEL] ? NSLog(@"responds") : NSLog(@"not responds");
Alse Im getting crash right after [invocation setSelector:viewWillAppearSEL];
. That's why I called topic title as Unexpected exception with NSInvocation.
UPDATED2:
Also my implementation of viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
Class parentViewController = [self superclass];
void (*superViewWillAppear)(id, SEL, BOOL) =(void(*)(id, SEL, BOOL))class_getMethodImplementation(parentViewController, _cmd);
superViewWillAppear(self, _cmd, animated);
NSLog(@"view will appear with super");
}