我知道Java https://stackoverflow.com/questions/3801343/what-is-string-pool-in-java and C# http://msdn.microsoft.com/en-us/library/system.string.intern两者在处理字符串文字时都使用字符串池来节省内存。
Objective-C 使用这样的机制吗?如果没有,为什么不呢?
是的,字符串文字就像@"Hello world"
永远不会被释放,并且它们指向相同的内存,这意味着指针比较是正确的。
NSString *str1 = @"Hello world";
NSString *str2 = @"Hello world";
if (str1 == str2) // Is true.
这也意味着弱字符串指针不会更改为 nil(普通对象会发生这种情况),因为字符串文字永远不会被释放。
__weak NSString *str = @"Hello world";
if (str == nil) // This is false, the str still points to the string literal
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)