[英]iPhone - difference between NSString and NSMutableString regarding memory usage
I have a block of text that have to be parsed. 我有一小段必须解析的文本。 It is kind a template like
像这样的模板
"Dear $name, we need the registration number of your $vehicle, bla bla"...
“亲爱的$ name,我们需要您的$ vehicle的注册号,bla bla bla” ...
imagine this 1000 characters long, with a lot of key variables, like $name, $vehicle, etc. 想象这1000个字符长,其中包含许多关键变量,例如$ name,$ vehicle等。
This text is stored on a #define
此文本存储在
#define
At run time, I have to parse this template and other 20 like that, replacing the key variables with the real values, like "Dear John, ....". 在运行时,我必须解析此模板和其他20个模板,将键变量替换为实际值,例如“ Dear John,....”。
I was using a NSString
variable to store the initial text and then these lines 我正在使用
NSString
变量存储初始文本,然后存储这些行
NSString *start = TEMPLATE1;
start = [start stringByReplacingOccurrencesOfString:NAME withString:realName];
start = [start stringByReplacingOccurrencesOfString:VEHICLE withString:realVehicle];
and so one and the code is working fast and well, but someone suggested using a NSMutableString
for the start variable, as it would use less memory. 如此一来,代码运行得很好,但是有人建议将
NSMutableString
用作start变量,因为它将使用较少的内存。
Is this correct? 这个对吗?
Will it worth the change? 值得改变吗?
It would be reasonable to do this: 这样做是合理的:
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])];
But if your code is already "working fast and well", I wouldn't bother changing it. 但是,如果您的代码已经“快速且良好地工作”,那么我就不会更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.