我想做点 hacky 的事情。
当我们尝试在未定义的类上调用方法时,我们通常会遇到错误,例如
// We get a undefined selector callback
[myClass someUndefinedMethod];
我想在 MyClass
中添加一些东西来捕获所有这些未定义的方法调用并进行处理。这可能吗?
我想要这样的东西,但它会拦截所有方法调用:
@implementation MyClass
- (void) performSelector(SEL):selector {
// Check if the method exists
if (![self respondsToSelector:selector]) {
// Handle unimplemeted selector
NSLog(@"No method called %@", selector);
}
// Otherwise proced as normal
else {
[super performSelector:selector];
}
}
@end
最佳答案
为什么不直接覆盖 doesNotRecognizeSelector:
NSObject 上的方法(假设你继承自它,你应该继承)?
关于objective-c - 是否有可能捕获所有选择器调用并在运行时重定向它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8534395/