PDA

View Full Version : NSString vs. NSMutableString, both can append strings?




fenrus110
Apr 7, 2008, 12:31 PM
what's the difference between these two code blocks? Which is preferable? For a single append, or multiple appends?

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

NSMutableString ms = [[NSMutableString alloc] initWithString:@"Hello"];
[ms appendString:@"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";



Sbrocket
Apr 7, 2008, 12:42 PM
what's the difference between these two code blocks? Which is preferable? For a single append, or multiple appends?

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

NSMutableString ms = [[NSMutableString alloc] initWithString:@"Hello"];
[ms appendString:@"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";

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:

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.

gnasher729
Apr 7, 2008, 12:45 PM
what's the difference between these two code blocks? Which is preferable? For a single append, or multiple appends?

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

NSMutableString ms = [[NSMutableString alloc] initWithString:@"Hello"];
[ms appendString:@"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";

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

fenrus110
Apr 7, 2008, 12:59 PM
Alright, thanks. I think I'll just stick with NSMutableString. Suits my high level thinking better.

Sbrocket
Apr 7, 2008, 01:11 PM
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:

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

sujithkrishnan
Jun 27, 2008, 07:40 AM
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:

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

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