objective-c 如何根据对象字符串属性在 Xcode 中设置条件断点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/988621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a conditional breakpoint in Xcode based on an object string property?
提问by Coocoo4Cocoa
I'm looking to be able to have the debugger break when it reaches a particular string match. As an example, I might have something like this:
我希望能够在达到特定字符串匹配时让调试器中断。举个例子,我可能有这样的事情:
Foo myObj = [self gimmeObj];
myObjmight have a property called name. I want the debugger to stop on the assignment when
myObj可能有一个名为name. 我希望调试器在分配时停止
[myObj.name isEqualToString:@"Bar"];
How can I set my conditional breakpoint in Xcode to do that?
如何在 Xcode 中设置我的条件断点来做到这一点?
回答by Peter N Lewis
You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).
您可以通过正常设置断点在 Xcode 中设置条件断点,然后按住 Control 键单击它并选择“编辑断点”(选择“运行”->“显示”->“断点”)。
In the breakpoint entry, there is a Condition column.
在断点条目中,有一个 Condition 列。
Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).
现在,有几个问题需要牢记在心。首先,gdb 不理解点语法,所以你必须使用 [myObj name] 代替 myObj.name(除非 name 是一个 ivar)。
Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:
接下来,与 gdb 中的大多数表达式一样,您必须告诉它返回结果的类型,即“BOOL”。所以设置一个条件,如:
(BOOL)[[myObj name] isEqualToString:@"Bar"]
Often it is actually easier to just do this in code by temporarily adding code like:
通常,通过临时添加以下代码在代码中执行此操作实际上更容易:
if ( [myObj.name isEqualToString:@"Bar"] ) {
NSLog( @"here" );
}
and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.
然后在NSLog上设置断点。那么你的条件可以任意复杂,而不必担心 gdb 可以解析什么,不能解析什么。
回答by nuynait
Here is how you do using XCode lldb conditional breakpoints.
以下是您如何使用 XCode lldb 条件断点。
First, double click the break point (or right click edit breakpoint), you can see a dialog popup.
首先,双击断点(或右键单击edit breakpoint),您可以看到一个对话框弹出。
Here is what those option means:
以下是这些选项的含义:
- Condition: The breakpoint will only fire under this condition.
- Ignore: The amount of times the condition needs to meet before fire the breakpoint
- Action: Action that runs after the breakpoint breaks.
- Options: Automatically continue after evaluating actions
- 条件:断点只会在此条件下触发。
- 忽略:触发断点之前条件需要满足的次数
- Action:断点中断后运行的操作。
- 选项:评估操作后自动继续
Here is a summary. For the above example in image, it means that when the variable buildingIdis equal to 13, break here. If I add ignore time to 1, then it will ignore the first time when buildingIdis equal to 13 and break at the second time the condition is met.
这是一个总结。对于上面图中的例子,意思是当变量buildingId等于13的时候,在这里打断。如果我将忽略时间添加到 1,那么它会在buildingId等于 13时忽略第一次并在第二次满足条件时中断。
For actions, when you press add actions, there will be a list of choice. Usually what I do is to use the Debugger Commandpoto print variables that I need to check and I believe that there are better ways using the actions then I do.
对于动作,当您按添加动作时,将出现一个选择列表。通常我所做的是使用Debugger Commandpo来打印我需要检查的变量,我相信有更好的方法来使用这些操作。
It seems that you have to recompile and run the app if you change the conditions at runtime
如果在运行时更改条件,似乎必须重新编译并运行应用程序
回答by Adam Rosenfield
I'm not sure if this will work, but you can try setting a breakpoint at that line of code, open up the debugger console (Cmd+Shift+R), and type
我不确定这是否可行,但您可以尝试在该行代码处设置断点,打开调试器控制台 (Cmd+Shift+R),然后键入
condition N (int)[[myObj name] isEqualToString:@"Bar"]
Where N is replaced by the number of the breakpoint (an integer).
其中 N 替换为断点的编号(整数)。
回答by Barry Wark
If you mutate myObj.name using the setter, you can add a symbolic breakpoint on -[MyObjClass setName:]either from the Debugger Console or from the Run->Manage Breakpoints->Add Symbolic Breakpoint menu in Xcode. If not (why not? you probably shouldn't be modifying the instance variable directly except in the designated initializer or dealloc) you can set a watchpoint in gdb (use the Debugger Console in Xcode once the debugger is running). Thispage explains how. I don't believe Xcode exposes a UI for setting watchpoints without using the Debugger Console.
如果您使用 setter 更改 myObj.name,您可以-[MyObjClass setName:]从调试器控制台或从 Xcode 中的 Run->Manage Breakpoints->Add Symbolic Breakpoint 菜单添加符号断点。如果没有(为什么不呢?除了在指定的初始化程序或 dealloc 中,您可能不应该直接修改实例变量)您可以在 gdb 中设置一个观察点(一旦调试器运行,使用 Xcode 中的调试器控制台)。此页面解释了如何。我不相信 Xcode 会在不使用调试器控制台的情况下公开用于设置观察点的 UI。
回答by lal
At times when working with Frameworks (debug builds) and need to put a breakpoint in certain file/location that is hard to navigate or isn't exposed publically in framework under development. One option is to write a helper class to trigger conditional breakpoints & make step-in/step-out easier.
有时在使用框架(调试版本)时需要在某些文件/位置放置断点,这些文件/位置难以导航或未在开发中的框架中公开。一种选择是编写一个辅助类来触发条件断点并使步进/步进更容易。
- (void)invokeFrameworkMethod {
...
[DebugConditionalBreakPointHelper breakPointCondition:YES comment:@"from invokeFrameworkMethod."];
...
}
Header declaration in framework under development.
正在开发的框架中的标头声明。
#import <Foundation/Foundation.h>
@interface DebugConditionalBreakPointHelper : NSObject
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment;
@end
And implementation file:
和实现文件:
#import "DebugConditionalBreakPointHelper.h"
@implementation DebugConditionalBreakPointHelper
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment {
if (enabled)
{
NSLog(@"Triggerred Conditional Break Point. Comment: %@");
}
}
@end


