Bar*_*vel 4 memory string pool objective-c string-literals
我知道Java和C#在处理字符串文字时都使用字符串池来节省内存.
Objective-C是否使用任何此类机制?如果没有,为什么不呢?
是的,像字符串文字@"Hello world"从未发布,它们指向相同的内存,这意味着指针比较是真的.
NSString *str1 = @"Hello world";
NSString *str2 = @"Hello world";
if (str1 == str2) // Is true.
Run Code Online (Sandbox Code Playgroud)
它还意味着弱字符串指针不会更改为nil(这适用于普通对象),因为字符串文字永远不会被释放.
__weak NSString *str = @"Hello world";
if (str == nil) // This is false, the str still points to the string literal
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
685 次 |
| 最近记录: |