简体   繁体   中英

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"...

imagine this 1000 characters long, with a lot of key variables, like $name, $vehicle, etc.

This text is stored on a #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, ....".

I was using a NSString variable to store the initial text and then these lines

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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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