xcode 如果类中的实例变量的值已更改,如何触发断点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18150221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:41:11  来源:igfitidea点击:

How do I have a breakpoint get triggered if an instance variable in the class has its value changed?

iosobjective-cxcodelldb

提问by Doug Smith

Say I have a variable, self.position, how do I get Xcode to break whenever it changes its value (a number of methods could change it).

假设我有一个变量,self.position当它改变它的值时我如何让 Xcode 中断(许多方法可以改变它)。

回答by Stavash

For conditional breaking:

对于有条件中断:

  1. Cmd+option click the breakpoint
  2. Add a break condition like so:
  1. Cmd+option 点击​​断点
  2. 添加一个中断条件,如下所示:

enter image description here

在此处输入图片说明

For breaking on every occasion the value has changed:

对于每次中断,该值都已更改:

  1. Implement trivial setter (and getter for the sake of clean code).
  2. Add breakpoint to setter.
  1. 实现简单的 setter(为了干净的代码而使用 getter)。
  2. 将断点添加到 setter。

If you want to see who invoked the setter - just look at the next line in the stack trace (viewDidLoad in my example):

如果您想查看谁调用了 setter - 只需查看堆栈跟踪中的下一行(在我的示例中为 viewDidLoad):

enter image description here

在此处输入图片说明

Update:

更新:

Adding a watchpoint

添加观察点

  1. Break anywhere so that the (lldb)prompt shows up in the console
  2. Type in watchpoint set variable _position(replace _position with an iVar you want to watch)
  3. Make a note of the assigned watchpoint number to your newly created watchpoint.
  4. Modify the watchpoint for conditional breaking: watchpoint modify -c "_position < 0.5" 1where the expression in quotes is the condition and the number at the end is the watchpoint number you noted in #3.
  5. Continue running. You'll break whenever the value matches the condition and you'll be able to inspect the stack frame to understand where the call came from.
  1. 在任何地方中断,以便(lldb)在控制台中显示提示
  2. 输入watchpoint set variable _position(用你想看的 iVar 替换 _position)
  3. 记下分配给新创建的观察点的观察点编号。
  4. 修改条件中断的观察点:watchpoint modify -c "_position < 0.5" 1其中引号中的表达式是条件,末尾的数字是您在 #3 中记录的观察点编号。
  5. 继续运行。只要值与条件匹配,您就会中断,并且您将能够检查堆栈帧以了解调用的来源。

回答by moonman239

Set a symbolic breakpoint. Go to the Breakpoint Navigator, click the +, click "Add Symbolic Breakpoint." In the first field, type -[YourSubclassNameHere setPosition:]", add any other setting you'd like to, then click outside the dialog.

设置符号断点。转到断点导航器,单击 +,单击“添加符号断点”。在第一个字段中,输入 -[YourSubclassNameHere setPosition:]",添加您想要的任何其他设置,然后在对话框外单击。

回答by geekchic

Well the simple way to do it is right clicking on the variable in the watch window and selecting the watch variable option. Xcode will then alert you when the value is changed.

那么简单的方法是右键单击观察窗口中的变量并选择观察变量选项。Xcode 会在值更改时提醒您。

Or you could have a look at Key-Value Observing.

或者你可以看看Key-Value Observing

回答by user2666713

You could override the setter of positionto have a breakpoint when it sets the variable.

您可以在设置position变量时覆盖设置器以设置断点。