问题
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).
回答1:
For conditional breaking:
- Cmd+option click the breakpoint
- Add a break condition like so:
For breaking on every occasion the value has changed:
- Implement trivial setter (and getter for the sake of clean code).
- Add breakpoint to setter.
If you want to see who invoked the setter - just look at the next line in the stack trace (viewDidLoad in my example):
Update:
Adding a watchpoint
- Break anywhere so that the
(lldb)
prompt shows up in the console - Type in
watchpoint set variable _position
(replace _position with an iVar you want to watch) - Make a note of the assigned watchpoint number to your newly created watchpoint.
- Modify the watchpoint for conditional breaking:
watchpoint modify -c "_position < 0.5" 1
where the expression in quotes is the condition and the number at the end is the watchpoint number you noted in #3. - 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.
回答2:
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.
回答3:
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.
Or you could have a look at Key-Value Observing.
回答4:
You could override the setter of position
to have a breakpoint when it sets the variable.
来源:https://stackoverflow.com/questions/18150221/how-do-i-have-a-breakpoint-get-triggered-if-an-instance-variable-in-the-class-ha