如何获取 NSObject 的引用计数? [英] How to get the reference count of an NSObject?
问题描述
有没有办法获取 NSObject(即 NSString)的当前引用计数?
Is there a way to fetch the current reference count for an NSObject (i.e. NSString)?
推荐答案
正如@Ben S 所说,它是 retainCount
方法.但是,您问错了问题,因为:
As @Ben S said, it's the retainCount
method. However, you're asking the wrong question, because:
重要:通常没有理由明确询问对象的保留计数是多少(请参阅retainCount).结果通常会产生误导,因为您可能不知道哪些框架对象保留了您感兴趣的对象.在调试内存管理问题时,您应该只关心确保您的代码遵守所有权规则.
Important: Typically there should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.
那么真正的问题是:你为什么需要知道?
So here's the real question: why do you need to know?
这篇关于如何获取 NSObject 的引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!