Does Objective-C use string pooling?

ε祈祈猫儿з 提交于 2019-12-10 15:42:28

问题


I know that Java and C# both use a string pool to save memory when dealing with string literals.

Does Objective-C use any such mechanism? If not, why not?


回答1:


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.

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


来源:https://stackoverflow.com/questions/11556714/does-objective-c-use-string-pooling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!