繁体   English   中英

iPhone-关于内存使用情况,NSString和NSMutableString之间的区别

[英]iPhone - difference between NSString and NSMutableString regarding memory usage

我有一小段必须解析的文本。 像这样的模板

“亲爱的$ name,我们需要您的$ vehicle的注册号,bla bla bla” ...

想象这1000个字符长,其中包含许多关键变量,例如$ name,$ vehicle等。

此文本存储在#define

在运行时,我必须解析此模板和其他20个模板,将键变量替换为实际值,例如“ Dear John,....”。

我正在使用NSString变量存储初始文本,然后存储这些行

NSString *start = TEMPLATE1;
start = [start stringByReplacingOccurrencesOfString:NAME withString:realName];
start = [start stringByReplacingOccurrencesOfString:VEHICLE withString:realVehicle];

如此一来,代码运行得很好,但是有人建议将NSMutableString用作start变量,因为它将使用较少的内存。
这个对吗?
值得改变吗?

这样做是合理的:

NSMutableString *text = [NSMutableString stringWithString:TEMPLATE1];
[text replaceOccurrencesOfString:NAME withString:realName options:0 range:NSMakeRange(0, [text length])];
[text replaceOccurrencesOfString:VEHICLE withString:realVehicle options:0 range:NSMakeRange(0, [text length])];

但是,如果您的代码已经“快速且良好地工作”,那么我就不会更改它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM