objective-c - 是否有可能捕获所有选择器调用并在运行时重定向它们

标签 objective-c

我想做点 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/

相关文章:

javascript - react native : How to export a method with a return value?

objective-c - 如何通过关系做Core Data查询?

objective-c - 转换为 (id) 以调用 Objective-C 中的任意方法

objective-c - Objective-C 中的阶乘代码

objective-c - 在第 3 代 iPad 中放大时删除和刷新 CATiledLayer

iphone - UIWebView 未加载移动网站

ios - 在没有 CATransaction begin&commit 的情况下禁用隐式动画

iOS - 如何选择数组中的对象比其他对象更多

ios - 嵌套 UINavigationController pushViewController 两次导致弹出动画问题

ios - 如何删除 NSMutableArray IOS 中除第一个索引以外的所有索引处的对象