以下代码引发异常。
vcClass
是一个 Class
对象(继承自UIViewController
)。Self
包含我的实现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];
带消息:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSInvocation setArgument:atIndex:]: index (1) out of bounds [-1, -1]
附加信息:iOS5、ARC。 有人可以解释我怎么了?
更新:
此代码代码给我响应消息。所以我的类对象是正确的 [vcClass instancesRespondToSelector:viewWillAppearSEL] 吗?NSLog(@"响应") : NSLog(@"不响应");
我也马上崩溃了[invocation setSelector:viewWillAppearSEL];
。这就是为什么我用 NSInvocation 将主题标题称为Unexpected 异常。
更新2:
还有我的实施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");
}