Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 7, 2008, 12:31 PM   #1
fenrus110
macrumors regular
 
Join Date: Mar 2008
NSString vs. NSMutableString, both can append strings?

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";
fenrus110 is offline   0 Reply With Quote
Old Apr 7, 2008, 12:42 PM   #2
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by fenrus110 View Post
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:
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.
__________________

Unibody MBP 15" | 2.53GHz | 4GB DDR3 | 320GB @ 7200RPM
Sbrocket is offline   0 Reply With Quote
Old Apr 7, 2008, 12:45 PM   #3
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by fenrus110 View Post
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".
gnasher729 is offline   0 Reply With Quote
Old Apr 7, 2008, 12:59 PM   #4
fenrus110
Thread Starter
macrumors regular
 
Join Date: Mar 2008
Alright, thanks. I think I'll just stick with NSMutableString. Suits my high level thinking better.
fenrus110 is offline   0 Reply With Quote
Old Apr 7, 2008, 01:11 PM   #5
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /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.
__________________

Unibody MBP 15" | 2.53GHz | 4GB DDR3 | 320GB @ 7200RPM
Sbrocket is offline   0 Reply With Quote
Old Jun 27, 2008, 07:40 AM   #6
sujithkrishnan
macrumors 6502
 
Join Date: May 2008
Location: Bangalore
Send a message via Yahoo to sujithkrishnan
Quote:
Originally Posted by Sbrocket View Post
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.
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 ????
sujithkrishnan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Don't laugh please. Is there any Mac app that can merge/append multi WMV files? matrix07 Mac Applications and Mac App Store 4 Apr 8, 2014 10:19 AM
Append Text to Text File in Applescript BobRon Mac Programming 2 Mar 23, 2014 02:52 PM
How to? Short cut to append text to doc\note (using alfred or other) kevindosi Mac Applications and Mac App Store 1 Jan 20, 2014 02:58 PM
Resolved: Need Help Figuring out Test Driven Development on a NSMutableString Category... ArtOfWarfare Mac Programming 0 Apr 7, 2013 11:15 AM
iPhone: springboard strings.. fisherking Jailbreaks and iOS Hacks 5 Feb 8, 2013 09:47 PM

Forum Jump

All times are GMT -5. The time now is 03:03 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC