如何获取NSObject的引用计数?

时间:2022-06-08 16:58:10

Is there a way to fetch the current reference count for an NSObject (i.e. NSString)?

有没有办法获取NSObject(即NSString)的当前引用计数?

3 个解决方案

#1


24  

As @Ben S said, it's the retainCount method. However, you're asking the wrong question, because:

正如@Ben S所说,这是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.

重要提示:通常,没有理由明确询问对象的保留计数是什么(请参阅retainCount)。结果往往会产生误导,因为您可能不知道哪些框架对象保留了您感兴趣的对象。在调试内存管理问题时,您应该只关心确保代码遵守所有权规则。

So here's the real question: why do you need to know?

所以这是真正的问题:为什么你需要知道?

#2


26  

retainCount

retainCount

But you should consider using CFGetRetainCount instead

但是你应该考虑使用CFGetRetainCount

#3


6  

using CFGetRetainCount function

使用CFGetRetainCount函数

Example :

示例:

print(CFGetRetainCount(object))

Read more here.

在这里阅读更多。

#1


24  

As @Ben S said, it's the retainCount method. However, you're asking the wrong question, because:

正如@Ben S所说,这是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.

重要提示:通常,没有理由明确询问对象的保留计数是什么(请参阅retainCount)。结果往往会产生误导,因为您可能不知道哪些框架对象保留了您感兴趣的对象。在调试内存管理问题时,您应该只关心确保代码遵守所有权规则。

So here's the real question: why do you need to know?

所以这是真正的问题:为什么你需要知道?

#2


26  

retainCount

retainCount

But you should consider using CFGetRetainCount instead

但是你应该考虑使用CFGetRetainCount

#3


6  

using CFGetRetainCount function

使用CFGetRetainCount函数

Example :

示例:

print(CFGetRetainCount(object))

Read more here.

在这里阅读更多。