In Objective-C, I would sometimes set watchpoints in LLDB to let me know when instance variables had changed. Can I do this with Swift properties too?
Right now, the only way I can achieve this is:
- adding a didSet handler to the property and setting a breakpoint inside (but this requires stopping the program and recompiling, which kind of defeats the purpose)
- adding a symbolic breakpoint on
[setPropertyName:]
but this only works if the class happens to support Objective-C bridging
Do I have any other options?
The answer was much simpler than I imagined. The easiest way to do this is to simply add a breakpoint on the property declaration. The debugger will break whenever the property is either read or written.
If, like me, you want to break only when the property is changed and ignore fetches, set a breakpoint on the property declaration, then go into the LLDB console and type "br list" to see a list of all your breakpoints:
As you can see, there's a master breakpoint "1" with three sub-breakpoints. Disable the sub-breakpoint for the getter:
and you're all set. The debugger will break only when that property is modified.
On XCode (8.2 Swift 3.0) set breakpoint normally on the swift property then run your app. After the app has ran go to breakpoint panel you can expand the breakpoint into multiple breakpoints:
All are selected by default, you can then disable the ones you don't need. NOTE: I found when you first add the breakpoint it will not expand until you run the app.
An alternative method you can try is to use lldb to add them. First add breakpoint somewhere inside your class instance eg viewDidLoad ext. p self and note the memory address of your instance.
Then add breakpoint like so where 0x0f0f0f0f0f0f is memory address of your class.