Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
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";
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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";

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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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";

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

macrumors regular
Original poster
Mar 24, 2008
142
0
Alright, thanks. I think I'll just stick with NSMutableString. Suits my high level thinking better.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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
 

sujithkrishnan

macrumors 6502
May 9, 2008
265
0
Bangalore
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

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 ????
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.