为什么要向@"Hi"发送retainCount?返回-1? [英] Why does sending retainCount to @"Hi" return -1?

查看:89
本文介绍了为什么要向@"Hi"发送retainCount?返回-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法retainCount应该返回一个无符号整数.

The method retainCount is supposed to return an unsigned integer.

为什么[@"Hi" retainCount]返回-1?

推荐答案

简单的答案是,由于@"Hi是字符串文字,因此它将始终位于二进制可执行映像中,并且永远不会消失" ,因此保留/释放无效,并且您看到的是UINT_MAX(在打印出带有%d的签名时看起来像-1).(请参阅Pete Kirkham关于具有这些语义的NSObjects的答案).

The simple answer is that because @"Hi" is a string literal, it will always be sitting around in the binary executable image, and will never "go away", thus retain/release have no effect, and you're seeing UINT_MAX (which looks like -1 when printed out signed eg with %d). (See Pete Kirkham's answer about NSObjects having these semantics).

除此之外,知道@@ Hi的行为类似于NSString*还是很有用的,它实际上是编译器创建的类<​​c5>的实例(或者可能是NSConstantString,我的调试器与某些文档不同意) ),它包装了文字字符串数据,并为您提供了NSString *接口.但是编译器知道这些字符串是特殊的,并且永远无法清除,因此它们将始终具有UINT_MAX(-1)的retainCount

Beyond this, it's useful to know that although @"Hi" behaves like an NSString*, it's actually a compiler-created instance of the class CFConstantString (or possibly NSConstantString, my debugger doesn't agree with some documentation), which wraps the literal string data and gives you the NSString* interface on it. But the compiler knows that these strings are special and can't ever get cleaned up, so they will always have a retainCount of UINT_MAX (-1)

这篇关于为什么要向@"Hi"发送retainCount?返回-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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