Question: When do I use NSString vs NSMutableString?
Answer: It depends. Let’s talk about each first.
NSString was created to set once and use. Any modification attempt to it really creates another separate string in memory.
NSMutableString assumes it can be changed and therefore it’s methods will work on the current string as it is in memory.
Now, remember, NSString is ‘immutable‘ and this is why we are saying you are really creating another one upon modifying….Having said that I have read people voicing their opinion that NSString may be faster if you kill the original one you had before modification. This is interesting because now we have ARC (Automatic Reference Counting) where you don’t have to worry about releasing memory you have allocated. Questions arise on performance and the influence of ARC. Wouldn’t the creators of the language have thought of speed though?
What comes to mind is- Why did the creators of Objective-C make:
NSString and NSMutableString
NSSet and NSMutableSet
NSArray and NSMutableArray…. and so on
Bottomline answer (for me) is:
If you know that a string will change then use NSMutableString. If you have a string that will be a constant throughout your iPhone app (like your company name or the title) then use NSString. I think the best practice is use the ‘Mutable‘ versions of the above for when you know they will change….because the creators of the language made it that way for good reasons.