I need to use a bunch of string variables throughout my program. I reassign some of them quite often, while others are stuck with the same value during execution.
What’s the best practice here?
In the first case, the variables should be NSMutableString and I should cast them to NSString (using the copy method) whenever they need to be arguments of functions that require NSString objects. Is that right?
When I reassign them to other constant values, I shouldn’t have to dispose the previous content, right?
As for NSString objects, if I need to assign a new value to them, I guess I should deallocate them, allocate them again, and then assign the new value. Is that correct?
Unless you’re actually modifying a string, you shouldn’t use
NSMutableString. You’re reassigning the whole string to a new value, so stay with a regularNSString. Use the autoreleased versions, because that’ll be more efficient than alloc/init/release all the time. You could also just reassign your strings to constants if you know what they’ll be assigned to.Well, you could do it that way, but it would be really inefficient. Remember inheritance—an
NSMutableStringis anNSString, just with some new stuff tacked on. A simple cast will do the trick:Even better though, you don’t even have to do that. Because of inheritance, you can directly pass in a mutable string wherever a regular string is required, no casting required. That’s the beauty of inheritance.
For neither mutable or immutable strings. Old values are simply overwritten in memory—nothing to dispose of there. As far as the memory management goes, it’s really not efficient to literally be creating new strings all the time. Just reassign them. You will never need to
alloc/initone string more than once, and that singleinitshould be balanced by a singlerelease.Addendum: When Should You Use Mutable?
A mutable string should be used when you are physically changing the value of the existing string, without completely discarding the old value. Examples might include adding a character to the beginning or the end, or changing a character in the middle. With a mutable string, you can do this “in place”—you’ll just modify the existing string. By contrast, an immutable string, once its value is set, cannot change that value.
NSStringhas methods such asstringByAppendingString:, which does add a string to an existing one—but it returns a new string. Behind the scenes,NSStringhas copied your old string to a new (larger) memory location, added the argument, and returned the new string. That copying is a lot less efficient (relatively speaking, or if you have to do it a lot).Of course, there’s nothing stopping you from physically assigning one string to another. Old values will be overwritten. Most
NSStrings, including the@"String Constants", are autoreleased. If you are creating a new string and you decide to alloc/init, you can then assign it to another value without consequence:You can do this with both mutable and immutable strings. The main takeaway is that you should only use mutable when your changing the string itself. But you can change the variable with both mutable and immutable strings without compiler or runtime issues (short of memory management, but most of it is autoreleased anyway).