如何打印对象的保留计数? [英] How to print the retain count of an object?

查看:92
本文介绍了如何打印对象的保留计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSLog在终端中打印出对象的保留计数.这是我的代码:

I am trying to print out the retain count of an object in the terminal using NSLog. Here is my code:

NSNumber *myInt=[[NSNumber alloc] initWithInteger: 100];
NSLog(@"myInt retain count=%d",[myInt retainCount]);

结果应该是1,但是我在终端上得到的是-1.我尝试使用%u而不是%d并最终得到4294967295.有人知道为什么会这样吗?

The result should be 1 but what I have got at the terminal is -1. I tried to use %u instead of %d and ended up getting 4294967295 as result. Does anyone know why this happens?

推荐答案

在@bbum到达这里之前,我要这样说:

Before @bbum gets here I get to say this:

不要依赖您代码中的-retainCount

Don't rely on -retainCount in your code

它不能给您期望的答案.事实证明答案-1在这里是正确的,您认为它不正确的事实是因为该框架正在做您不知道的事情.使用自动引用计数(ARC),或者如果必须使用手动保留/释放,则只需遵循内存管理准则即可,而不使用-retainCount方法.

It doesn't give you answers you expect. It turns out that the answer -1 is correct here, the fact that you don't think it's correct is because the framework is doing something you don't know about behind your back. Use automatic reference counting (ARC), or if you must use manual retain/release just follow the memory management guidelines without using the -retainCount method.

这篇关于如何打印对象的保留计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆