Using NSMutableString* for NSString* result
Using NSMutableString* for NSString* result
- Subject: Using NSMutableString* for NSString* result
- From: Matt Gough <email@hidden>
- Date: Fri, 03 Oct 2003 16:28:54 +0100
Dear all,
(I am just starting out in Cocoa)
I have a function that returns an NSString*. Internally I use an
NSMutableString to build up the result value.
Is it OK to just return (NSString*)myMutableString, or do I have to do copy
the string in to a 'proper' NSString? (It is assumed that the function has
no knowledge as to how its result will be used elsewhere)
i.e. given the two versions below, which is considered more correct by those
of you with more experience of this sort of stuff?
Example 1:
-(NSString*) getSomeFancyString
{
NSMutableString* s = [NSMutableString new];
[s appendString:getSomeOtherString()];
... Other stuff to add/change the string
return (NSString*)[s autorelease];
}
Example 2:
-(NSString*) getSomeFancyString
{
NSMutableString* s = [NSMutableString new];
[s appendString:getSomeOtherString()];
... Other stuff to add/change the string
NSString* result = [NSString stringWithString:s];
[s release];
return result;
}
Thanks for any help
Matt Gough
_______________________________________________
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.