NSObject 描述和 debugDescription

发布于 2025-01-07 20:50:25 字数 150 浏览 0 评论 0原文

我已经看到出于调试目的提到了描述和调试描述,但还没有看到关于何时使用哪个以及在什么条件下它们可能会产生不同结果的精确解释。

NSObject 的文档也没有关于 debugDescription 的任何内容。问题:什么时候使用哪个,在什么条件下,它们的输出应该/会不同?

I have seen mentioning of description and debugDescription for debugging purposes, but have yet seen precise explanations of when to use which and under what conditions they may produce different results.

NSObject's documentation also doesn't have anything on debugDescription. Question: When to use which, and under what condition, should/would their output be different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心 2025-01-14 20:50:25

技术说明 TN2124

注意:print-object实际上调用了指定对象的debugDescription方法。 NSObject 通过调用描述方法来实现此方法。因此,默认情况下,对象的调试描述与其描述相同。但是,如果您想解耦这些,您可以覆盖 debugDescription;许多 Cocoa 对象都这样做。

如果您实现了 debugDescription,则在 GDB 中打印该对象将显示其结果。知道 UI 中使用了 description (我知道绑定可以做到这一点),您可能希望使用它来打印一些用户不需要看到的附加信息。

Technical Note TN2124

Note: print-object actually calls the debugDescription method of the specified object. NSObject implements this method by calling through to the description method. Thus, by default, an object's debug description is the same as its description. However, you can override debugDescription if you want to decouple these; many Cocoa objects do this.

If you have debugDescription implemented, printing the object in GDB will display its result. Knowing that description is used in UI (I know bindings do that), you may want to use this to print some additional information that user doesn't need to see.

锦欢 2025-01-14 20:50:25

对已经讲过的内容进行补充。

如果您想在 lldb 中使用 po 时改进输出,您可以重写 debugDescription 方法。请记住,打印 self(对象本身)将调用描述方法。如果由于某种原因 不适合您,也可以覆盖该方法。

所以我在这里要强调的是,打印 self 将调用 description 方法,而 po 调用 debugDescription ,默认情况下调用description。有了这个,您就可以区分该调用的结果。

One addition to what have already been told.

If you want to improve the output while working with po in lldb you can override the debugDescription method. Just keep in mind that printing self (the object itself) will call description method. If for some reason <ClassName: objectAddress> is not good for you also override that method.

So my point here was to highlight that printing self will call description method, whereas po calls debugDescription which by default calls description. Giving this you can differentiate the results of that calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文