Re: Glueing strings together...
Re: Glueing strings together...
- Subject: Re: Glueing strings together...
- From: lbland <email@hidden>
- Date: Tue, 13 Jan 2004 10:57:13 -0500
On Jan 13, 2004, at 10:07 AM, Michael Becker wrote:
I have a loop in which I need to glue strings together. Like this:
for (i=0; i<[ theArray count]; i++) {
myString = [ myString stringByAppendingFormat:"\n%@", [ theArray
objectAtIndex:i]];
}
This works well. My only concern is memory leaking. I am creating a
new string and setting my pointer to this new string, but does the old
(original) string get released?
What is the proper way to build a string out of many parts?
hi-
try this (totally untested!)
id pool = [[NSAutoreleasePool alloc] init];
unsigned count = [ theArray count];
id oldString = myString;
for (i=0; i < count; i++)
{
myString = [ myString stringByAppendingFormat:"\n%@", [ theArray
objectAtIndex:i]];
}
if(count > 0)
{
[oldString release]; // do you want this?
[myString retain];
}
[pool release];
I think stringByAppendingFormat: should return an autorelease
string(?), so you need a retain at the end. The pool keeps the temps
localized. You can probably also use a NSMutableString and append right
into it without explicit temps.
I have to wonder about the documentation's description of appendFormat:
though:
appendFormat:
- (void)appendFormat:(NSString *)format, ...
Adds a constructed string to the receiver. Creates the new string using
NSStrings stringWithFormat: method with the arguments listed.
good luck-
-lance
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.