Objective-C是否使用字符串池? [英] Does Objective-C use string pooling?
本文介绍了Objective-C是否使用字符串池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 Java 和
I know that Java and C# both use a string pool to save memory when dealing with string literals.
Objective-C是否使用任何此类机制?如果没有,为什么不呢?
Does Objective-C use any such mechanism? If not, why not?
推荐答案
是的,从不释放像@"Hello world"
这样的字符串文字,它们指向相同的内存,这意味着指针比较是正确的.
Yes, string literals like @"Hello world"
are never released and they point to the same memory which means that pointer comparison is true.
NSString *str1 = @"Hello world";
NSString *str2 = @"Hello world";
if (str1 == str2) // Is true.
这也意味着弱字符串指针不会更改为nil(在正常对象中会发生这种情况),因为字符串文字不会被释放.
It also means that a weak string pointer won't change to nil (which happens for normal objects) since the string literal never gets released.
__weak NSString *str = @"Hello world";
if (str == nil) // This is false, the str still points to the string literal
这篇关于Objective-C是否使用字符串池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文