English 中文(简体)
能否监测或打印在目标C中发送到对象的电文? [重复]
原标题:Can the messages sent to an object in Objective-C be monitored or printed out? [duplicate]
This question already has answers here:
Closed 11 years ago.

The community reviewed whether to reopen this question 2 years ago and left it closed:

原先的近似原因未解决

Possible Duplicate:
Intercept method call in Objective-C
How to log all methods used in iOS app

例如,iOS中 UIViewcurrent 对象在向用户显示其视图之前收到许多信息:

  1. viewWillAppear
  2. viewWillLayoutSubviews
  3. viewDidLayoutSubviews
  4. viewDidAppear
  5. ...

因为框架源代码无法查看, 我们必须依靠书籍或博客, 或者有办法打印或监测(1) 目标C 或(2) 以任何工具发送到此对象的所有信息吗?

最佳回答

我使用(并仍在使用)的最佳方式是呼吁:

(void)instrumentObjcMessageSends(YES);

当我需要开始记录所有信息时,然后:

(void)instrumentObjcMessageSends(NO);

Don t forget to add #import <objc/runtime.h>.
When I don t need it anymore. The annoying thing is that the log is created under /tmp/msgSends- and this means that you have to open the terminal and use tail to see it in a readable way.

印刷的东西是这样的:

- CustomTableViewController UIViewController _parentModalViewController
- CustomTableViewController UIViewController isPerformingModalTransition
- CustomTableViewController UIViewController setInAnimatedVCTransition:
- CustomTableViewController UIViewController viewWillMoveToWindow:
- CustomTableViewController UIViewController isPerformingModalTransition
- CustomTableViewController UIViewController parentViewController
- CustomTableViewController UIViewController _popoverController
- CustomTableViewController UIViewController _didSelfOrAncestorBeginAppearanceTransition
- CustomTableViewController UIViewController parentViewController
- CustomTableViewController UIViewController __viewWillDisappear:
- CustomTableViewController UIViewController _setViewAppearState:isAnimating:
- CustomTableViewController UIViewController automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers

注意 : 自从我上次使用此方法以来已有一段时间了, 它看起来似乎没有记录私有方法子分类。 所以, 如果您有一个 < code> DummyClass , 带有 < code> -( 避免)_ dummyMethod 私有的 < dummySubClass , 然后有一个 < code> -( 避免)_ dummy Method 执行的 < dummy Method >, 消息将不会登录 。

< 加固> 对于 iOS, 这只对模拟器有效 。

问题回答

您可以使用 DTrace 来监测运行中的应用程序, 以查看被调用的方法和类别。 您可以在命令行使用 DTrace 来很容易地监测在模拟器中运行的 iOS 应用程序。 首先, 您需要使用 < code> ps 找到应用程序的 PID, 然后您就可以运行一个 Dtrace 探测器, 如 :

sudo dtrace -q -n  objc1234:::entry { printf("%s %s
", probemod, probefunc); }  

其中 1234 是应用程序的处理代号 。

这将产生看起来如下的产出:

UIStatusBarItemView -isVisible
UIStatusBarLayoutManager -_positionAfterPlacingItemView:startPosition:
UIView(Geometry) -frame
CALayer -frame
UIStatusBarLayoutManager -_startPosition
UIView(Geometry) -bounds
CALayer -bounds
UIStatusBarItemView -standardPadding
UIStatusBarItem -appearsOnLeft
UIStatusBarItem -leftOrder

如果您只有兴趣追踪单类,例如 UIView , 您可以使用:

sudo dtrace -q -n  objc1234:UIView::entry { printf("%s %s
", probemod, probefunc); } 

如果您想要追踪所有类中 dealloc 的所有电话, 您将会使用 :

sudo dtrace -q -n  objc1234::-dealloc:entry { printf("%s %s
", probemod, probefunc); } 

显然,您可以将这些组合起来,只看到 UIView dealloc :

sudo dtrace -q -n  objc1234:UIView:-dealloc:entry { printf("%s %s
", probemod, probefunc); } 

如果您想要能够区分某类的特定对象, 您也可以使用下列方式打印对象的内存地址( self ):

sudo dtrace -q -n  objc1234:UIView:-dealloc:entry { printf("%s (0x%p) %s
", probemod, arg0, probefunc); } 

戴特瑞斯的力量非常强大 比我在这里展示的要大得多





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...