是否可以在Swift属性上设置观察点? [英] Is it possible to set watchpoints on Swift properties?
问题描述
在Objective-C中,有时我会在LLDB中设置观察点,以让我知道实例变量何时更改.我也可以使用Swift属性来做到这一点吗?
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:
- 在属性中添加didSet处理程序并在其中设置断点(但这需要停止程序并重新编译,这会破坏目的)
- 在
[setPropertyName:]
上添加符号断点,但这仅在该类恰好支持Objective-C桥接的情况下有效
- 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
我还有其他选择吗?
推荐答案
答案比我想象的简单得多.最简单的方法是在属性声明上添加一个断点.每当读取或写入属性时,调试器都会中断.
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.
如果像我一样,您只想在更改属性时中断并且忽略获取,请在属性声明上设置断点,然后进入LLDB控制台并键入"br list"以查看所有断点的列表:
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:
(lldb) br list
Current breakpoints:
1: file = '/Users/testuser/Desktop/TestFoo/Test.swift', line = 12, locations = 3, resolved = 3, hit count = 1
1.1: where = TestFoo`TestFoo.Test.x.getter : Swift.Int + 12 at Test.swift:12, address = 0x00000001084cfefc, resolved, hit count = 1
1.2: where = TestFoo`TestFoo.Test.x.setter : Swift.Int + 16 at Test.swift:12, address = 0x00000001084cff80, resolved, hit count = 0
1.3: where = TestFoo`TestFoo.Test.x.materializeForSet : Swift.Int + 16 at Test.swift:12, address = 0x00000001084d00f0, resolved, hit count = 0
如您所见,有一个主断点"1"和三个子断点.禁用getter的子断点:
As you can see, there's a master breakpoint "1" with three sub-breakpoints. Disable the sub-breakpoint for the getter:
(lldb) br disable 1.1
1 breakpoints disabled.
您已经准备就绪.仅当修改该属性时,调试器才会中断.
and you're all set. The debugger will break only when that property is modified.
这篇关于是否可以在Swift属性上设置观察点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!