NSString vs. NSMutableString, both can append strings?

Discussion in 'Mac Programming' started by fenrus110, Apr 7, 2008.

  1. macrumors regular

    Joined:
    Mar 24, 2008
    #1
    what's the difference between these two code blocks? Which is preferable? For a single append, or multiple appends?

    NSString s = [[NSString alloc] initWithString:mad:"Hello"];
    s = [s stringByAppendingString:mad:"World"];

    NSMutableString ms = [[NSMutableString alloc] initWithString:mad:"Hello"];
    [ms appendString:mad:"World"];

    Oh, I'm a bit of a String newb, so if there are better ways to write these, do tell. I find doing all this writing kinda annoying when I'm used to doing "hello" + "world";
     
  2. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #2
    Both of these, functionally, do the same thing except for one difference - the top code block leaks. -[NSString stringByAppendingString:] generates a new immutable NSString object which you then tell the pointer s to point to. In the process, however, you orphan the NSString object that s originally pointed to. Replacing the line as follows would get rid of the leak:
    Code:
    s = [[s autorelease] stringByAppendingString:@"World"];
    
    Really, if you're doing a lot of string appending its just easier to use NSMutableString. I don't know of any specific performance gains off-hand because I've never needed to delve that far into optimization yet, but since NSMutableString is designed to be just that, mutable, I'm sure there's some performance optimizations that you'd gain versus creating a new NSString object every time you append something to a string. Maybe someone else here knows exactly what gains there are or aren't with using NSMutableString over NSString.
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    The first one creates a string containing the text "Hello" and stores a pointer to the string into s, then asks that string to create another string with the text "HelloWorld". The pointer to the second string is also stored in s, so the first pointer is lost, which means that without garbage collection enabled, you have a memory leak.

    The second example creates a mutable string with the text "Hello", then modifies that string. The same string object now contains the text "HelloWorld".
     
  4. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    Alright, thanks. I think I'll just stick with NSMutableString. Suits my high level thinking better.
     
  5. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #5
    Just make sure you remember with the memory allocation stuff...using a method with "new", "init", or "copy" in the name means that you're taking ownership of that object. Whenever you take ownership of an object its your code's responsibility to relinquish ownership of it at some point...be that during your dealloc method or at some other point in the code.

    For example, say you had a code snippet that looked like this:
    Code:
    -(id)init {
    	if (![super init])
    		return nil;
    
    	s = [[NSString alloc] initWithString:@""];
    }
    
    -(void)dealloc {
    	[s release];
    	[super dealloc];
    }
    
    -(void)appendStringOntoStringIvar:(NSString*)aString {
    	s = [[[s autorelease] stringByAppendingString:aString] retain];
    }
    
    Everything here is balanced, and ownership is taken and released as they should be. Each method has a balanced number of retain and release/autorelease calls.

    Sorry if I'm harping, but I've been picking up a lot of this stuff pretty quickly over the last couple weeks and its fundamentals like this that I wish I had known when I started...so I try and pass it on to others. :p
     
  6. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #6
    HI SB....

    So can i conclude that my memory-management is perfect by following the "BALANCING" you mentioned above, regardless of the assignments and whatever other operations on the object ????
     

Share This Page