ios - 有没有办法观察在对象(iOS)上调用的每个消息调用?

标签 ios selector nsinvocation message-forwarding

每次向对象发送消息时,我只想获取选择器名称、参数、发送者或 NSInvocation 实例。可能的?类似于 forwardInvocation: 的东西,但在每一种情况下(每个方法调用)。

最佳答案

有一种方法可以通过使用 objective-c 消息传递中的隐藏参数来获取选择器的名称和目标。来自 Apple's documentation :

When objc_msgSend finds the procedure that implements a method, it calls the procedure and passes it all the arguments in the message. It also passes the procedure two hidden arguments:

The receiving object The selector for the method

所以在一个方法中你可以得到以下内容:

   id  target = getTheReceiver();
   SEL method = getTheMethod();

如果仍然不能满足您的需求,您可以执行以下操作:

  1. 创建一个名为 Helper 的类.
  2. 添加对将调用方法的类的引用,格式如下:id <HelperDelegate> myClassReference;
  3. 当你需要做一个[self method]而是创建一个 Helper 的实例类并在其上调用方法,如 [helper method];并添加这个 [helper setMyClassReference:self]; .
  4. 应用程序应该会崩溃,但随后只需添加 forwardInvocation:Helper 上类(class)。从那里,您将能够获得 NSInvocation目的。做你需要做的,然后:[anInvocation invokeWithTarget:myClassReference];这样您就可以将消息传递给原始调用者。

P.S:即使这不能回答您的问题,也请为该问题 +1。这真的让我开始思考这个问题。

关于ios - 有没有办法观察在对象(iOS)上调用的每个消息调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245261/

相关文章:

ios - 在 TableView 单元格中使用自定义 XIB

ios - 如何正确拆除多点连接 session ?

Android,查看背景选择器,执行任务的正确方法是什么?

objective-c - 为什么prepareWithInitationTarget是: specific to NSUndoManager?

ios - 不使用 Xcode 的 Flutter 项目中 iOS 的文件夹引用

objective-c - 在 Xcode Storyboard 中使用 Unwind Segue 关闭 Popover

jquery - 使用 JQuery 从正文中选择所有输入文本

javascript - 如何使用它获得选择器

IOS NSMethodSignature 保持为零

iphone - Dispatch Queue 和 NSOperationQueue 的区别