-[NSString UTF8String]的保证生命周期是多少? [英] What is the guaranteed lifecycle of -[NSString UTF8String]?

查看:102
本文介绍了-[NSString UTF8String]的保证生命周期是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档含糊地说,它会在需要的时间内保持使用,但是实际上什么时候才释放它呢?原始NSString何时被释放?当前的自动释放池何时清除?还有其他时间吗?此外,与当前的实现方式相比,何时可以保证使用?如果它在NSString的生存期内保持不变,是否每次都会返回相同的指针?

The docs vaguely say that it will stick around as long as needed, but when is it actually deallocated? When the original NSString is deallocated? When the current autorelease pool clears? Sometime else? Furthermore, when is it guaranteed to stay for, as opposed to the current implementation? If it stays for the lifetime of the NSString, will the same pointer be returned every time?

谢谢

推荐答案

更新30/11/2018

正如David Dunham在评论中指出的那样,Apple文档已更改(我的答案中的旧链接现在已断开).

As David Dunham pointed out in comments, the Apple documentation has changed (the old link in my answer is now broken).

Apple文档现在说:

此C字符串是指向字符串对象内部结构的指针,该对象的生存期可能短于字符串对象,并且肯定不会具有更长的生存期.因此,如果需要将C字符串存储在使用此属性的内存上下文之外,则应该复制C字符串.

This C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. Therefore, you should copy the C string if it needs to be stored outside of the memory context in which you use this property.

我的原始答案现在有两个部分是错误的.

My original answer is now wrong in two parts.

我的说法是,当前的Apple文档明确拒绝了UTF8字符串可能在NSString的解除分配后仍然存在的问题.

My claim that the UTF8 string could possibly survive the deallocation of the NSString is explicitly denied by the current Apple documentation.

在Apple文档中,自动发布上下文"已替换为内存上下文".我不确定这到底是什么意思,但是在退出获取指针的范围之后,您可能不应该信任该指针.

In the Apple docs "Autorelease context" has been replaced by "memory context". I'm not sure what that means exactly, but you probably should not trust the pointer after you have exited the scope in which it was acquired.

原始答案

返回的C字符串会自动释放,就像释放返回的对象一样;如果需要将C字符串存储在创建C字符串的自动发布上下文之外,则应复制C字符串.

The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created.

对我来说,这似乎并不模糊.

That seems pretty non vague to me.

NB您不必真正担心当前的实现方式.实施更改

NB You shouldn't really worry about what the current implementation does. Implementations change

根据以上内容,您的问题的答案可能是:

Extrapolating the above, the answers to your questions are probably:

但是它什么时候真正被释放?

but when is it actually deallocated?

自动释放池耗尽时.

当原始NSString被释放时?

When the original NSString is deallocated?

不.该文档暗示您可以分配一个字符串,使用UTF8String然后释放该字符串,并且UTF8String仍然有效,但请注意

No. The documentation implies you could alloc a string, take the UTF8String then release the string and the UTF8String would still be valid but be aware of this line from the Memory Management Rules:

通常可以保证接收到的对象在接收到的方法中仍然有效...尽管您还必须注意修改从另一个接收到对象的对象.

A received object is normally guaranteed to remain valid within the method it was received ... although you must also take care if you modify an object from which you received another object.

修改对象"可能包括释放从中获取UTF8字符串的NSString.

"Modifying an object" might include releasing the NSString you got the UTF8 string from.

当前自动释放池何时清除?

When the current autorelease pool clears?

这就是文档所说的.

有时候呢?

Sometime else?

否.

此外,与当前的实现方式相比,还能保证何时保留?

Furthermore, when is it guaranteed to stay for, as opposed to the current implementation?

如果没有记录,则不能保证.

If it's not documented, it's not guaranteed.

如果它在NSString的生存期内保持不变,是否每次都会返回相同的指针?

If it stays for the lifetime of the NSString, will the same pointer be returned every time?

我对此表示怀疑.对于不可变的字符串,可能会缓存UTF8字符串,但可能不会.为什么不编写一些代码来尝试呢?无论如何,您都不能在生产代码中使用它,也不能在您的NSString可能确实是NSMutableString的任何情况下使用它.

I doubt it. Maybe for immutable strings, the UTF8 string is cached, but maybe not. Why not write some code to try it out? In any case, you can't rely on it in production code, nor in any situation where your NSString might really be an NSMutableString.

这篇关于-[NSString UTF8String]的保证生命周期是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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