I read that if you use stringByAppendingString on an NSString, you get leaks because the pointer associated with the initial NSString moves to point to the new line created by add, whereas with NSMutableString, your pointer always points to this mutable string.
This sounds like advice from someone who doesn't quite understand what is happening with memory management. Of course, [NSString stringByAppendingString] returns a new string. But what you do with this new line is up to you. You could, of course, cause a memory leak by reassigning the result to the stored property in a carefree manner, for example:
myStringProperty = [myStringProperty stringByAppendingString:@" more bits"];
The correct form would be to use self, for example:
self.myStringProperty = [myStringProperty stringByAppendingString:@" more bits"];
Follow the memory recommendations for cocoa.
As for dictionaries and other types of collections: refer to what is obtained from the dictionary corresponding to the type you know. If you pull out an object that is actually an NSString, but try to use it as an NSMutableString, your application will crash (with a “selector not found” or similar). Therefore, in this case, you need to create a new NSMutableString from NSString.
Interesting note: Apple decided to make NSMutableString a subclass of NSString. Something about this seems unreasonable to me - if something seems immutable, because it has an NSString type, I want it to be immutable! (But actually it could be NSMutableString.) Compare this to Java, which has a String class and a completely separate BufferedString class.
source share